python3使用xpath爬内涵吧段子

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
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容