怎么用Python可视化显示电影的口碑和票房数据
本篇内容主要讲解"怎么用Python可视化显示电影的口碑和票房数据",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"怎么用Python可视化显示电影的口碑和票房数据"吧!
数据获取
1.评分数据
网页分析
查看网页源代码,可以见到目标数据在标签
- ,通过xpath解析就可以获取。下面直接上代码!
编程实现
headers = { 'Host':'movie.douban.com', 'user-agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3947.100 Safari/537.36', 'cookie':'bid=uVCOdCZRTrM; douban-fav-remind=1; __utmz=30149280.1603808051.2.2.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not%20provided); __gads=ID=7ca757265e2366c5-22ded2176ac40059:T=1603808052:RT=1603808052:S=ALNI_MYZsGZJ8XXb1oU4zxzpMzGdK61LFA; dbcl2="165593539:LvLaPIrgug0"; push_doumail_num=0; push_noty_num=0; __utmv=30149280.16559; ll="118288"; __yadk_uid=DnUc7ftXIqYlQ8RY6pYmLuNPqYp5SFzc; _vwo_uuid_v2=D7ED984782737D7813CC0049180E68C43|1b36a9232bbbe34ac958167d5bdb9a27; ct=y; ck=ZbYm; __utmc=30149280; __utmc=223695111; __utma=30149280.1867171825.1603588354.1613363321.1613372112.11; __utmt=1; __utmb=30149280.2.10.1613372112; ap_v=0,6.0; _pk_ref.100001.4cf6=%5B%22%22%2C%22%22%2C1613372123%2C%22https%3A%2F%2Fwww.douban.com%2Fmisc%2Fsorry%3Foriginal-url%3Dhttps%253A%252F%252Fmovie.douban.com%252Fsubject%252F34841067%252F%253Ffrom%253Dplaying_poster%22%5D; _pk_ses.100001.4cf6=*; __utma=223695111.788421403.1612839506.1613363340.1613372123.9; __utmb=223695111.0.10.1613372123; __utmz=223695111.1613372123.9.4.utmcsr=douban.com|utmccn=(referral)|utmcmd=referral|utmcct=/misc/sorry; _pk_id.100001.4cf6=e2e8bde436a03ad7.1612839506.9.1613372127.1613363387.', } url="https://movie.douban.com/cinema/nowplaying/zhanjiang/"r = requests.get(url,headers=headers)r.encoding = 'utf8's = (r.content)selector = etree.HTML(s)li_list = selector.xpath('//*[@id="nowplaying"]/div[2]/ul/li')dict = {}for item in li_list: name = item.xpath('.//*[@class="stitle"]/a/@title')[0].replace(" ","").replace("\n","") rate = item.xpath('.//*[@class="subject-rate"]/text()')[0].replace(" ", "").replace("\n", "") dict[name] = float(rate) print("电影="+name) print("评分="+rate) print("-------")
电影名和评分数据已经爬取下来,并且降序排序,后面会进行可视化。
2.时长和电影类型
网页分析
在页面源码中,电影时长的网页标签是roperty="v:runtime",电影类型的网页标签对应是property="v:genre"
编程实现
###时长def getmovietime(): url = "https://movie.douban.com/cinema/nowplaying/zhanjiang/" r = requests.get(url, headers=headers) r.encoding = 'utf8' s = (r.content) selector = etree.HTML(s) li_list = selector.xpath('//*[@id="nowplaying"]/div[2]/ul/li') for item in li_list: title = item.xpath('.//*[@class="stitle"]/a/@title')[0].replace(" ", "").replace("\n", "") href = item.xpath('.//*[@class="stitle"]/a/@href')[0].replace(" ", "").replace("\n", "") r = requests.get(href, headers=headers) r.encoding = 'utf8' s = (r.content) selector = etree.HTML(s) times = selector.xpath('//*[@property="v:runtime"]/text()') type = selector.xpath('//*[@property="v:genre"]/text()') print(title) print(times) print(type) print("-------")
3.评论数据
网页分析
查看网页代码,评论数据的目标标签是
(不知道如何分析,可以看上一篇文章python爬取44130条用户观影数据,分析挖掘用户与电影之间的隐藏信息!,这篇文章也是分析豆瓣电影,里面有详细介绍)。
下面开始爬取这七部电影的评论数据!!!!
编程实现
###评论数据def getmoviecomment(): url = "https://movie.douban.com/cinema/nowplaying/zhanjiang/" r = requests.get(url, headers=headers) r.encoding = 'utf8' s = (r.content) selector = etree.HTML(s) li_list = selector.xpath('//*[@id="nowplaying"]/div[2]/ul/li') for item in li_list: title = item.xpath('.//*[@class="stitle"]/a/@title')[0].replace(" ", "").replace("\n", "") href = item.xpath('.//*[@class="stitle"]/a/@href')[0].replace(" ", "").replace("\n", "").replace("/?from=playing_poster", "") print("电影=" + title) print("链接=" + href) ### with open(title+".txt","a+",encoding='utf-8') as f: for k in range(0,200,20): url = href+"/comments?start="+str(k)+"&limit=20&status=P&sort=new_score" r = requests.get(url, headers=headers) r.encoding = 'utf8' s = (r.content) selector = etree.HTML(s) li_list = selector.xpath('//*[@class="comment-item "]') for items in li_list: text = items.xpath('.//*[@class="short"]/text()')[0] f.write(str(text)+"\n") print("-------") time.sleep(4)
将这些评论数据分别保存到文本文件中,后面将这些评论数据采用不同的图形进行可视化展示!!!!
数据可视化
1.评分数据可视化
###画图font_size = 10 # 字体大小fig_size = (13, 10) # 图表大小data = ([datas])# 更新字体大小mpl.rcParams['font.size'] = font_size# 更新图表大小mpl.rcParams['figure.figsize'] = fig_size# 设置柱形图宽度bar_width = 0.35index = np.arange(len(data[0]))# 绘制评分rects1 = plt.bar(index, data[0], bar_width, color='#0072BC')# X轴标题plt.xticks(index + bar_width, itemNames)# Y轴范围plt.ylim(ymax=10, ymin=0)# 图表标题plt.title(u'豆瓣评分')# 图例显示在图表下方plt.legend(loc='upper center', bbox_to_anchor=(0.5, -0.03), fancybox=True, ncol=5)# 添加数据标签def add_labels(rects): for rect in rects: height = rect.get_height() plt.text(rect.get_x() + rect.get_width() / 2, height, height, ha='center', va='bottom') # 柱形图边缘用白色填充,纯粹为了美观 rect.set_edgecolor('white')add_labels(rects1)# 图表输出到本地plt.savefig('豆瓣评分.png')
在热映的这七部电影中,《你好,李焕英》评分最高(8.3),《唐人街探案3》最低(5.8),这有点出乎意料(唐人街探案3热度远比你好,李焕英热度要高)。
2.时长和类型可视化
时长数据可视化
#####时长可视化itemNames.reverse()datas.reverse()# 绘图。fig, ax = plt.subplots()b = ax.barh(range(len(itemNames)), datas, color='#6699CC')# 为横向水平的柱图右侧添加数据标签。for rect in b: w = rect.get_width() ax.text(w, rect.get_y() + rect.get_height() / 2, '%d' % int(w), ha='left', va='center')# 设置Y轴纵坐标上的刻度线标签。ax.set_yticks(range(len(itemNames)))ax.set_yticklabels(itemNames)plt.title('电影时长(分钟)', loc='center', fontsize='15', fontweight='bold', color='red')#plt.show()plt.savefig("电影时长(分钟)")
图中的电影时长均在120分钟左右
最长的电影《唐人街探案3》(136分钟),时长最短的是《熊出没·狂野大陆》(99分钟)
电影类型数据可视化
#####2.类型可视化###从小到大排序dict = sorted(dict.items(), key=lambda kv: (kv[1], kv[0]))print(dict)itemNames = []datas = []for i in range(len(dict) - 1, -1, -1): itemNames.append(dict[i][0]) datas.append(dict[i][1])x = range(len(itemNames))plt.plot(x, datas, marker='o', mec='r', mfc='w', label=u'电影类型')plt.legend() # 让图例生效plt.xticks(x, itemNames, rotation=45)plt.margins(0)plt.subplots_adjust(bottom=0.15)plt.xlabel(u"类型") # X轴标签plt.ylabel("数量") # Y轴标签plt.title("电影类型统计") # 标题plt.savefig("电影类型统计.png")
将这七部电影的类型进行统计(有的电影属于多个类型,比如'动作', '奇幻', '冒险')。七部电影中其中有四部是属于喜剧。科幻、犯罪、悬疑、冒险均属于其中一部。
3.评论数据词云可视化
使用七种不同的图案进行词云可视化,因此将绘图的代码封装成函数!!!
####词云代码def jieba_cloud(file_name, icon): with open(file_name, 'r', encoding='utf8') as f: text = f.read() text = text.replace('\n',"").replace("\u3000","").replace(",","").replace("。","") word_list = jieba.cut(text) result = ">
开始对这七部电影评论数据进行绘图
###评论数据词云def commentanalysis(): lists = ['刺杀小说家','你好,李焕英','人潮汹涌','侍神令','唐人街探案3','新神榜:哪吒重生','熊出没·狂野大陆'] for i in range(0,len(lists)): title =lists[i]+".txt" jieba_cloud(title , (i+1))
到此,相信大家对"怎么用Python可视化显示电影的口碑和票房数据"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!