给我编写一个数据爬取可视化
时间: 2024-05-17 08:14:02 浏览: 17
好的,以下是一个简单的 Python 代码示例,用于爬取豆瓣电影 Top 250 的数据并进行可视化:
```python
import requests
from bs4 import BeautifulSoup
import pandas as pd
import matplotlib.pyplot as plt
# 定义函数,用于爬取数据
def get_data():
url = 'https://movie.douban.com/top250?start='
movies = []
for i in range(0, 250, 25):
req = requests.get(url + str(i))
soup = BeautifulSoup(req.text, 'html.parser')
items = soup.find_all('div', class_='item')
for item in items:
title = item.find('span', class_='title').text
rating = item.find('span', class_='rating_num').text
movies.append({'title': title, 'rating': rating})
return movies
# 爬取数据
movies = get_data()
# 将数据转换成 DataFrame 格式
df = pd.DataFrame(movies)
# 统计电影评分的分布情况
bins = [0, 6, 7, 8, 9, 10]
labels = ['0-6', '6-7', '7-8', '8-9', '9-10']
df['rating_range'] = pd.cut(df['rating'].astype(float), bins=bins, labels=labels)
rating_counts = df['rating_range'].value_counts()
# 绘制柱状图
plt.bar(rating_counts.index, rating_counts.values)
plt.xlabel('Rating Range')
plt.ylabel('Counts')
plt.title('Distribution of Movie Ratings')
plt.show()
```
以上代码爬取豆瓣电影 Top 250 的数据,并统计电影评分的分布情况,最后将结果绘制成柱状图进行可视化。这只是一个简单的示例,您可以根据自己的需求修改代码,爬取不同的数据,并进行不同的可视化。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)