美文网首页
python3使用xpath爬内涵吧段子

python3使用xpath爬内涵吧段子

作者: 我心有猛虎2018 | 来源:发表于2019-01-18 22:29 被阅读0次
import requests
from lxml import etree
headers = {'user-agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'}

def download(url):
    reponse = requests.get(url,headers = headers)
    reponse.encoding = 'gbk'
    text = reponse.text
    html = etree.HTML(text)
    results = html.xpath('//*[@class=\'piclist longList\']//a[@href!=\'/article/\']//text()')
    for line in results:
        print(line)
    duanzs = html.xpath('//*[@class=\'piclist longList\']//div[@class=\'f18 mb20\']//text()')
    for linedata in duanzs:
        print(linedata.strip())
        print('*'*20)
download("https://www.neihan8.com/article/list_5_2.html")
BaiduShurufa_2019-1-18_22-30-43.png

相关文章

网友评论

      本文标题:python3使用xpath爬内涵吧段子

      本文链接:https://www.haomeiwen.com/subject/rdiodqtx.html