request请求但脚本爬取

发布时间 2023-08-28 22:56:05作者: khalil12138
import requests
from lxml import etree

url = "https://duanzixing.com/"
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 Edg/116.0.1938.62',
}
res = requests.get(url, headers=headers)
data = res.content.decode()


tree = etree.HTML(data)
artical_list = tree.xpath('//div[@class="content"]/article[@class="excerpt"]')

for artical in artical_list:
title = artical.xpath('./header/h2/a/text()')[0]
content = artical.xpath('./p[@class="note"]/text()')[0]
print(title, content)