python爬虫豆瓣电影top250进行可视化
时间: 2023-11-01 11:08:30 浏览: 168
好的,这个任务可以分为两个部分,第一部分是爬取豆瓣电影top250的数据,第二部分是对数据进行可视化。
首先,我们可以使用Python的requests和BeautifulSoup库来进行网页爬取和数据解析。
```python
import requests
from bs4 import BeautifulSoup
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'}
res = requests.get(url, headers=headers)
soup = BeautifulSoup(res.text, 'html.parser')
movie_list = soup.find('ol', class_='grid_view').find_all('li')
movies = []
for movie in movie_list:
title = movie.find('span', class_='title').text
rating = movie.find('span', class_='rating_num').text
director = movie.find('p').text.split('\n')[1].strip().split(':')[1]
actors = movie.find('p').text.split('\n')[2].strip().split(':')[1]
year = movie.find('span', class_='inq').text if movie.find('span', class_='inq') else ''
movies.append({'title': title, 'rating': rating, 'director': director, 'actors': actors, 'year': year})
return movies
movies = get_movies(url)
print(movies)
```
接下来是可视化部分,我们可以使用Python的matplotlib库来进行数据可视化。
```python
import matplotlib.pyplot as plt
import pandas as pd
# 将数据转换为DataFrame格式
df = pd.DataFrame(movies)
# 统计电影导演的出现次数
director_count = df['director'].value_counts().sort_values(ascending=False)[:10]
director_count.plot(kind='bar')
plt.title('Top 10 Directors of Douban Movie Top 250')
plt.xlabel('Director')
plt.ylabel('Number of Movies')
plt.show()
```
这段代码将统计豆瓣电影top250中出现次数最多的10位导演,并将结果可视化为柱状图。
完整代码如下:
```python
import requests
from bs4 import BeautifulSoup
import matplotlib.pyplot as plt
import pandas as pd
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'}
res = requests.get(url, headers=headers)
soup = BeautifulSoup(res.text, 'html.parser')
movie_list = soup.find('ol', class_='grid_view').find_all('li')
movies = []
for movie in movie_list:
title = movie.find('span', class_='title').text
rating = movie.find('span', class_='rating_num').text
director = movie.find('p').text.split('\n')[1].strip().split(':')[1]
actors = movie.find('p').text.split('\n')[2].strip().split(':')[1]
year = movie.find('span', class_='inq').text if movie.find('span', class_='inq') else ''
movies.append({'title': title, 'rating': rating, 'director': director, 'actors': actors, 'year': year})
return movies
movies = get_movies(url)
# 将数据转换为DataFrame格式
df = pd.DataFrame(movies)
# 统计电影导演的出现次数
director_count = df['director'].value_counts().sort_values(ascending=False)[:10]
director_count.plot(kind='bar')
plt.title('Top 10 Directors of Douban Movie Top 250')
plt.xlabel('Director')
plt.ylabel('Number of Movies')
plt.show()
```
阅读全文