爬虫-图书列表数据

点击查看代码
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)


posted @ 2024-10-04 09:47  神仙不在  阅读(13)  评论(0)    收藏  举报