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")
python3使用xpath爬内涵吧段子_第1张图片
BaiduShurufa_2019-1-18_22-30-43.png

你可能感兴趣的:(python3使用xpath爬内涵吧段子)