python爬虫数据可视化图
时间: 2023-12-02 21:05:40 浏览: 86
Python爬虫数据可视化示例流程
以下是一个Python爬虫数据可视化的例子:
```python
# 导入需要用到的库
import requests
from bs4 import BeautifulSoup
import pandas as pd
import matplotlib.pyplot as plt
# 爬取数据
url = 'https://movie.douban.com/top250'
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')
movies = soup.find_all('div', class_='info')
# 处理数据
titles = []
scores = []
for movie in movies:
title = movie.find('span', class_='title').text
score = movie.find('span', class_='rating_num').text
titles.append(title)
scores.append(float(score))
# 可视化分析
df = pd.DataFrame({'title': titles, 'score': scores})
df = df.sort_values(by='score', ascending=False)
df_top10 = df[:10]
plt.bar(df_top10['title'], df_top10['score'])
plt.xticks(rotation=90)
plt.title('Top 10 Movies on Douban')
plt.xlabel('Movie Title')
plt.ylabel('Score')
plt.show()
```
该例子爬取了豆瓣电影Top250的数据,并使用matplotlib库绘制了Top10电影的柱状图。你可以根据自己的需求修改代码,实现不同的数据可视化效果。
阅读全文