编写一个python程序,爬取豆瓣平台的电影信息,并进行数据分析,然后通过可视化界面展示。

时间: 2024-03-07 14:51:26 浏览: 17
好的,下面是一个爬取豆瓣电影信息并进行数据分析并通过可视化界面展示的Python程序: ```python import requests from bs4 import BeautifulSoup import pandas as pd import matplotlib.pyplot as plt import tkinter as tk from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg # 爬取豆瓣电影TOP250的信息 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.select('.item') # 存储电影信息 movie_list = [] for movie in movies: name = movie.select_one('.title').text.strip() rating = movie.select_one('.rating_num').text.strip() director = movie.select_one('.bd p:nth-of-type(1)').text.strip().split(' ')[1] actors = movie.select_one('.bd p:nth-of-type(1)').text.strip().split(' ')[2:] actors = '/'.join(actors) year = movie.select_one('.bd p:nth-of-type(1)').text.strip().split(' ')[-2] country = movie.select_one('.bd p:nth-of-type(1)').text.strip().split(' ')[-1].replace('(', '').replace(')', '') summary = movie.select_one('.bd p:nth-of-type(2)').text.strip() movie_list.append({'name': name, 'rating': float(rating), 'director': director, 'actors': actors, 'year': year, 'country': country, 'summary': summary}) # 将电影信息存储到CSV文件 movies_df = pd.DataFrame(movie_list) movies_df.to_csv('movies.csv', index=False) # 对电影评分进行统计分析 rating_counts = movies_df['rating'].value_counts() rating_counts = rating_counts.reset_index() rating_counts.columns = ['rating', 'count'] rating_counts = rating_counts.sort_values('rating') # 对电影导演进行统计分析 director_counts = movies_df['director'].value_counts()[:10] director_counts = director_counts.reset_index() director_counts.columns = ['director', 'count'] director_counts = director_counts.sort_values('count', ascending=False) # 创建GUI界面 root = tk.Tk() root.title('Douban Movie Analysis') # 创建电影评分的图表 fig1 = plt.figure(figsize=(6, 4), dpi=100) ax1 = fig1.add_subplot(111) ax1.bar(rating_counts['rating'], rating_counts['count']) ax1.set_title('Douban Movie Rating Counts') ax1.set_xlabel('Rating') ax1.set_ylabel('Count') canvas1 = FigureCanvasTkAgg(fig1, master=root) canvas1.draw() canvas1.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1) # 创建电影导演的图表 fig2 = plt.figure(figsize=(6, 4), dpi=100) ax2 = fig2.add_subplot(111) ax2.bar(director_counts['director'], director_counts['count']) ax2.set_title('Top 10 Movie Directors in Douban TOP250') ax2.set_xlabel('Director') ax2.set_ylabel('Count') canvas2 = FigureCanvasTkAgg(fig2, master=root) canvas2.draw() canvas2.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1) root.mainloop() ``` 这个程序使用了Requests库和BeautifulSoup库来爬取豆瓣电影TOP250的信息,并使用Pandas库对电影评分和导演进行统计分析。接着,使用Matplotlib库将统计结果绘制成图表,并使用Tkinter库创建一个GUI界面,将图表展示出来。 注意:在运行程序之前,需要安装所需要的库,如Requests、BeautifulSoup、Pandas、Matplotlib和Tkinter等。

相关推荐

最新推荐

recommend-type

Python爬取数据并实现可视化代码解析

主要介绍了Python爬取数据并实现可视化代码解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
recommend-type

python 爬取豆瓣电影评论,并进行词云展示

# -*-coding:utf-8-*- import urllib.request from bs4 import BeautifulSoup def getHtml(url): """获取url页面""" headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, ...
recommend-type

Python爬取股票信息,并可视化数据的示例

今天带大家爬取雪球平台的股票数据, 并且实现数据可视化 先看下效果图 基本环境配置 python 3.6 pycharm requests csv time 目标地址 https://xueqiu.com/hq 爬虫代码 请求网页 import requests url = '...
recommend-type

Python爬虫爬取电影票房数据及图表展示操作示例

主要介绍了Python爬虫爬取电影票房数据及图表展示操作,结合实例形式分析了Python爬虫爬取、解析电影票房数据并进行图表展示操作相关实现技巧,需要的朋友可以参考下
recommend-type

(二)爬取新房销售信息——数据分析+可视化篇

上一个任务通过requests、BeautifulSoup4两个功能强大、用法简洁的函数库已经获取到了楼盘名、地址和价格这些新房信息,并且保存为csv格式文件,csv文件可以用excel打开并进行编辑。 当然爬取数据只是第一步,“白嫖...
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

spring添加xml配置文件

1. 创建一个新的Spring配置文件,例如"applicationContext.xml"。 2. 在文件头部添加XML命名空间和schema定义,如下所示: ``` <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。