import requests
from lxml import etree
import csv
url = "https://book.douban.com/tag/%E5%B0%8F%E8%AF%B4"
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36'
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
html = etree.HTML(response.text)
books = html.xpath('//li[@class="subject-item"]')
with open('豆瓣图书.csv', 'w', newline='', encoding='utf-8-sig') as file:
writer = csv.writer(file)
writer.writerow(['书名', 'URL链接', '作者、出版社、出版时间、价格'])
for book in books[:10]:
title = book.xpath('.//h2/a/@title')[0]
link = book.xpath('.//h2/a/@href')[0]
pub_info = book.xpath('.//div[@class="pub"]/text()')[0]
writer.writerow([title, f'https://book.douban.com{link}', pub_info.strip()])
else:
print("请求失败,状态码:", response.status_code)
3-40
于 2024-10-10 18:42:24 首次发布

被折叠的 条评论
为什么被折叠?



