python爬虫豆瓣电影并做可视化界面
时间: 2023-07-19 20:50:59 浏览: 112
好的,下面是一个简单的示例代码,可以爬取豆瓣电影top250的电影名称和评分,并将数据可视化为一个直方图:
```python
import requests
from bs4 import BeautifulSoup
import matplotlib.pyplot as plt
url = 'https://movie.douban.com/top250'
def get_movies(url):
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')
movie_list_soup = soup.find('ol', attrs={'class': 'grid_view'})
movie_list = []
for movie_li in movie_list_soup.find_all('li'):
detail = movie_li.find('div', attrs={'class': 'hd'})
movie_name = detail.find('span', attrs={'class': 'title'}).getText()
rating = movie_li.find('span', attrs={'class': 'rating_num'}).getText()
movie_list.append({'name': movie_name, 'rating': float(rating)})
return movie_list
movies = get_movies(url)
ratings = [movie['rating'] for movie in movies]
plt.hist(ratings, bins=20)
plt.xlabel('Rating')
plt.ylabel('Frequency')
plt.title('Distribution of ratings for top 250 movies on Douban')
plt.show()
```
这段代码使用了requests库和BeautifulSoup库来获取豆瓣电影top250的电影名称和评分信息,并使用matplotlib库将评分数据可视化为一个直方图。您可以根据您的需求进行修改和扩展。请注意,爬取网站的时候应该遵守网站的爬取规则和法律法规。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.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)