点击查看代码
import requests
from lxml import etree
url = 'http://antpython.net/webspider/douban_book_list.html'
headers = {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36 Edg/129.0.0.0"
}
response = requests.get(url, headers=headers)
html_etree = etree.HTML(response.text)
divs = html_etree.xpath('//div[@class="card book"]')
for div in divs:
name = ''.join(div.xpath('./div/div/div[2]/h5/text()'))
n_url = ''.join(div.xpath('./div/div/div[2]/a/@href'))
print(name, n_url)