用python抓取豆瓣电影top250,写入数据库,并实现可视化
时间: 2023-08-15 18:33:48 浏览: 111
基于python抓取豆瓣电影TOP250的数据及进行分析.zip
5星 · 资源好评率100%
好的,以下是抓取豆瓣电影Top250并写入数据库的Python代码:
```python
import requests
from bs4 import BeautifulSoup
import sqlite3
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'}
movies = []
for i in range(0, 250, 25):
params = {'start': str(i), 'filter': ''}
response = requests.get(url, headers=headers, params=params)
soup = BeautifulSoup(response.text, 'html.parser')
movie_list = soup.find('ol', class_='grid_view').find_all('li')
for movie in movie_list:
title = movie.find('span', class_='title').text
rating_num = movie.find('span', class_='rating_num').text
quote = movie.find('span', class_='inq').text if movie.find('span', class_='inq') else ''
movies.append({'title': title, 'rating_num': rating_num, 'quote': quote})
conn = sqlite3.connect('movies.db')
cursor = conn.cursor()
cursor.execute('CREATE TABLE IF NOT EXISTS top250 (title TEXT, rating_num REAL, quote TEXT)')
for movie in movies:
cursor.execute('INSERT INTO top250 (title, rating_num, quote) VALUES (?, ?, ?)', (movie['title'], movie['rating_num'], movie['quote']))
conn.commit()
conn.close()
```
这段代码会使用 requests 库向豆瓣电影Top250的网站发送请求,然后使用 BeautifulSoup 库对网页进行解析,最后把电影的标题、评分和引用语存储到 SQLite 数据库中。
接下来是实现可视化的Python代码,使用了 Matplotlib 库:
```python
import sqlite3
import matplotlib.pyplot as plt
conn = sqlite3.connect('movies.db')
cursor = conn.cursor()
cursor.execute('SELECT rating_num, COUNT(*) FROM top250 GROUP BY rating_num')
data = cursor.fetchall()
conn.close()
x = [d[0] for d in data]
y = [d[1] for d in data]
plt.bar(x, y)
plt.xlabel('Rating')
plt.ylabel('Count')
plt.title('Top 250 Movies on Douban')
plt.show()
```
这段代码会从 SQLite 数据库中读取每个评分的电影数量,然后使用 Matplotlib 库绘制条形图来展示。运行后会弹出一个可视化窗口,展示所有评分的电影数量。
阅读全文