用Python爬取当当网数据可视化
时间: 2023-11-05 15:49:41 浏览: 249
好的,下面是一个简单的Python代码,用于爬取当当网的图书数据,并用matplotlib库进行可视化:
```python
import requests
from bs4 import BeautifulSoup
import matplotlib.pyplot as plt
# 爬取当当网图书数据
url = 'http://search.dangdang.com/?key=%BB%AA%CE%AA&act=input'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
books = soup.select('.bigimg > li')
# 统计不同评分的图书数量
ratings = {}
for book in books:
rating = book.select_one('.search_star_line span')['style']
if 'width' in rating:
rating = int(float(rating.split(':')[1][:-1]) / 20)
if rating in ratings:
ratings[rating] += 1
else:
ratings[rating] = 1
# 可视化图书评分分布
plt.bar(ratings.keys(), ratings.values())
plt.xlabel('Rating')
plt.ylabel('Number of Books')
plt.title('Distribution of Book Ratings on Dangdang')
plt.show()
```
这段代码首先使用requests和BeautifulSoup库爬取当当网的图书数据,然后统计不同评分的图书数量,并使用matplotlib库将其可视化为柱状图。你可以将“key=%BB%AA%CE%AA”替换为你需要搜索的关键词。
阅读全文