爬取当当网图书畅销榜
时间: 2024-02-10 11:08:41 浏览: 162
以下是使用Python爬取当当网图书畅销榜的代码示例:
```python
import requests
from bs4 import BeautifulSoup
# 发送请求获取网页内容
url = "https://book.douban.com/top250"
response = requests.get(url)
html_content = response.text
# 使用BeautifulSoup解析网页内容
soup = BeautifulSoup(html_content, "html.parser")
# 找到图书列表
book_list = soup.find_all("div", class_="pl2")
# 遍历图书列表并提取信息
for book in book_list:
# 提取图书标题
title = book.find("a").text.strip()
# 提取图书作者和出版信息
author_info = book.find("p", class_="pl").text.strip().split("/")
author = author_info[0].strip()
publisher = author_info[-3].strip()
# 提取图书评分
rating = book.find("span", class_="rating_nums").text.strip()
# 打印图书信息
print("标题:", title)
print("作者:", author)
print("出版社:", publisher)
print("评分:", rating)
print("--")
```
阅读全文