jytlalala
jytlalala
2020-01-14 10:58
采纳率: 100%
浏览 1.6k

用python提取div里的文本

import requests
from bs4 import BeautifulSoup
import pprint
import json
url="http://www.miaomu.com/qyml/default.asp"
r= requests.get(url)
html=r.content.decode('gbk','ignore')
soup=BeautifulSoup(html,"html.parser")
articles=soup.find_all("div",{"class":"gyjtnr"})
articles图片说明

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • m0_37886429
    幸福丶如此 2020-01-14 13:30
    已采纳

    可以参考下

    # -*- coding:utf-8 -*-
    
    import requests
    from lxml import etree
    from pprint import pprint
    
    def get_html(url):
        headers = {"User-Agent":"Mozilla/5.0 (compatible; MSIE 9.0; AOL 9.0; Windows NT 6.0; Trident/5.0)",}
        r = requests.get(url,headers=headers)
        return r.content.decode('gb2312','ignore')
    
    def parse_html(text):
        infos = {}
        html = etree.HTML(text)
        datas = html.xpath("//div[@class='gynr']/div[@class='gyjtnr']")
        for index,data in enumerate(datas,1):
            name = html.xpath("//div[@class='gynr']/p[%s]//b/text()" % index)[0]
            content = ''.join(data.xpath("./text()"))
            if name not in infos.keys():
                infos[name] = content
        return infos
    
    
    if __name__ == '__main__':
        url="http://www.miaomu.com/qyml/default.asp"
        html = get_html(url=url)
        if html:
            infos = parse_html(text=html)
            pprint(infos)
    
    
    点赞 评论
  • weixin_44336969
    'ゞBibo。 2020-01-14 12:57

    所以你是问什么...

    点赞 评论
  • m0_45333735
    YDHTB爱编程vb.net 2020-04-06 07:51

    遍历tag_name为div的,然后打印.text

    点赞 评论

相关推荐