用python爬虫抓取豆瓣2022年度读书榜单
时间: 2023-11-17 20:09:02 浏览: 211
以下是使用Python爬虫抓取豆瓣2022年度读书榜单的示例代码:
```python
import requests
from bs4 import BeautifulSoup
url = 'https://book.douban.com/latest?icn=index-latestbook-all'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
book_list = soup.find_all('div', class_='detail-frame')
for book in book_list:
title = book.find('div', class_='title').text.strip()
author = book.find('div', class_='author').text.strip()
score = book.find('div', class_='score').text.strip()
print(title, author, score)
```
这段代码使用requests库向豆瓣读书最新书籍页面发送请求,然后使用BeautifulSoup库解析HTML页面,最后找到书籍的标题、作者和评分信息并打印输出。
阅读全文