当前位置:   article > 正文

Python实现输入电影名字自动生成豆瓣评论词云图(带GUI界面)小程序_电影电视gui界面

电影电视gui界面

Python实现输入电影名字自动生成豆瓣评论词云图(带GUI界面)小程序

一、项目背景

电影逐渐成为人们生活的不可或缺的一部分,而了解一部电影的可以通过电影评分与大众推荐度,但以上的方式都太过于片面,了解一部电影的方法是通过已经观看完电影的人群的反馈,虽然电影评分和大众推荐度在一定程度上是观影人群的反馈,但是并没有电影评论的反馈真实。评论对影视剧的好坏与特色可以更加充分的体现。所以了解一部影视作品的最好方式是通过评论。出于对评论的大量且参差不齐的考虑,通过出现的高频词来分析,是通过评论了解影视剧较为便捷的方式。将高频词整合,通过词云图是极好的方式。所以项目基于以上背景决定基于网络爬虫获取豆瓣评论生成词云图实现指定电影豆瓣评论关键词词云生成器。

二、项目的详细设计

  • 生成器的主要结构为,爬虫模块、词云生成模块、GUI界面模块。
  • 主要流程为在GUI输入电影名称后,将参数传给爬虫模块,具体通过get方式将电影名称封装成url发送至豆瓣服务器进入搜索界面获取指定电影URL中指定的id,将获取的id再次封装成url发送至豆瓣服务器进入评论网页,通过爬虫爬取评论数据存至本地生成文本格式。生成本地文本后,程序将调用词云模块对生成的文本提取,并进行符号删除并且删去停用词。并定位背景图以及设置图片词语参数生成词云图返回。
  • 考虑到人工收集影评较为复杂且工作量繁重,故通过网络爬虫实现对指定豆瓣评论的爬取。爬取对象为豆瓣影评取,且需要考虑输入电影名字爬取指定的功能,所以选用的库函数有:requests、urllib.request、Beautifulsoup、用于爬取评论数据及获取。将电影名称封装url发送后,进入搜索界面,使用Xpath定位HTML代码中的搜索结果的第一项中的sid。
  • 将获取到的的id构造url发送给豆瓣服务器,访问评论网页。进入网站后,调用定义函数getComment()对评论进行爬取,并保存至本地文本中。
    后续通过词云处理后生成词云图展示。
爬虫模块
  • 以上为爬虫工作所有需要调用的包
import urllib.request
import requests, re
from bs4 import BeautifulSoup
  • 1
  • 2
  • 3
  • 获取HTML源码
def getHtml(url):
    """获取url页面"""
    headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36'}
    req = urllib.request.Request(url,headers=headers)
    req = urllib.request.urlopen(req)
    content = req.read().decode('utf-8')
    return content

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 这部分代码用于将获取后的HTML源码处理并提取出指定内容。
def getComment(url):
    """解析HTML页面"""
    html = getHtml(url)
    soupComment = BeautifulSoup(html, 'html.parser')
    comments = soupComment.findAll('span', 'short')
    onePageComments = []
    for comment in comments:
        # print(comment.getText()+'\n')
        onePageComments.append(comment.getText()+'\n')
    return onePageComments
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 在搜索页面内定位第一个展示的电影对应的sid(即为豆瓣网里每一部电影唯一的id)
def getid(name):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.75 Safari/537.36',
    }
    movie_name = name
    params = {
        "q": movie_name
    }
    search_url = "https://www.douban.com/search"
    r = requests.get(search_url, params=params, headers=headers)
    soup = BeautifulSoup(r.content, 'lxml')
    first_movie_info = soup.find('a', {'class': 'nbg'})['onclick']
    pattern = re.compile('\d{4,}')
    sid = str(pattern.search(first_movie_info).group())
    return(sid)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 将上一个函数返回的结果进行评论提取并存入txt文本
def get_data():
    with open('电影评论.txt', 'w', encoding='utf-8') as f:
        sid=getid(_input.get())
        for page in range(10):  # 豆瓣爬取多页评论需要验证。
            url = 'https://movie.douban.com/subject/'+sid+'/comments?start=' + str(20*page) + '&limit=20&sort=new_score&status=P'


            for i in getComment(url):
                f.write(i)
                
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
GUI模块
  • GUI模块调用的库
from tkinter import *
import matplotlib.pyplot as plt
  • 1
  • 2
app = Tk()#主窗口
_input = Entry()#输入框
_input.pack()
app.title("电影评论关键词生成器")
screenwidth = app.winfo_screenwidth()#定义窗口宽度
screenheight = app.winfo_screenheight()#定义窗口高度
dialog_width = 400
dialog_height = 170
# 前两个参数是窗口的大小,后面两个参数是窗口的位置
app.geometry(
    "%dx%d+%d+%d" % (dialog_width, dialog_height, (screenwidth - dialog_width) / 2, (screenheight - dialog_height) / 2))#设置窗口局中分布
btn = Button(text='查询', command=get_data,width=10)#定义按钮,按钮的结果是调用get data函数进入爬虫模块
btn.place(x=155, y=80)#定义按钮位置
btn.pack()
app.mainloop()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
词云模块
  • 词云模块调用的库
from wordcloud import WordCloud
import pandas as pd
from imageio import imread
import jieba
  • 1
  • 2
  • 3
  • 4
with open("电影评论.txt", "r", encoding='UTF-8') as fin1:
        all_words = cut_words(fin1)#读取文本
        #定义停用词
        stop = ['的','你','了','将','为','例',' ','多','再','有','是','等','天','次','让','在','我','也','就','这样','啊','和','都','《','》',',','看','!','什么','怎么','这么','很','给','没有','不是','说'
        ,'不','吗','?','!' ,'?','。' ,'...' ,'电影','主','男','女'   ]
        words_cut = []#定义停用词
        for word in all_words:
            if word not in stop:
                words_cut.append(word)
        word_count = pd.Series(words_cut).value_counts()
        back_ground = imread("F:\\flower.jpg")#自己定义图片位置
        wc = WordCloud(
                       font_path="C:\\Windows\\Fonts\\simhei.ttf", #设置字体
                       background_color="white",  #设置词云背景颜色
                       max_words=400,  #词云允许最大词汇数
                       mask=back_ground,  #词云形状
                       max_font_size=400,   #最大字体大小
                       random_state=90  #配色方案的种数
                      )
        wc1 = wc.fit_words(word_count)  #生成词云
        plt.figure()
        plt.imshow(wc1)
        plt.axis("off")
        plt.show()
        wc.to_file("ciyun.png")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

三、项目的分析与测试

界面如下:
输入电影名称(采用《八佰》作为示例)

在这里插入图片描述
在这里插入图片描述

返回结果:
在这里插入图片描述

结果分析:
成功将豆瓣社区中《八佰》的评论区出现的高频次进行生成词云图并返回。

全部代码

from tkinter import *
import urllib.request
import requests, re
from bs4 import BeautifulSoup
from wordcloud import WordCloud
import pandas as pd
from imageio import imread
import matplotlib.pyplot as plt
import jieba
def getHtml(url):
    """获取url页面"""
    headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36'}
    req = urllib.request.Request(url,headers=headers)
    req = urllib.request.urlopen(req)
    content = req.read().decode('utf-8')
    return content
def cut_words(top_search):
    top_cut=[]
    for top in top_search:
        top_cut.extend(list(jieba.cut(top)))  #使用精确模式切割词汇
    return top_cut
def getComment(url):
    """解析HTML页面"""
    html = getHtml(url)
    soupComment = BeautifulSoup(html, 'html.parser')
    comments = soupComment.findAll('span', 'short')
    onePageComments = []
    for comment in comments:
        # print(comment.getText()+'\n')
        onePageComments.append(comment.getText()+'\n')
    return onePageComments
def getid(name):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.75 Safari/537.36',
    }
    movie_name = name
    params = {
        "q": movie_name
    }
    search_url = "https://www.douban.com/search"
    r = requests.get(search_url, params=params, headers=headers)
    soup = BeautifulSoup(r.content, 'lxml')
    first_movie_info = soup.find('a', {'class': 'nbg'})['onclick']
    pattern = re.compile('\d{4,}')
    sid = str(pattern.search(first_movie_info).group())
    return(sid)
def get_data():
    with open('电影评论.txt', 'w', encoding='utf-8') as f:
        sid=getid(_input.get())
        for page in range(10):  # 豆瓣爬取多页评论需要验证。
            url = 'https://movie.douban.com/subject/'+sid+'/comments?start=' + str(20*page) + '&limit=20&sort=new_score&status=P'


            for i in getComment(url):
                f.write(i)


    with open("电影评论.txt", "r", encoding='UTF-8') as fin1:
        all_words = cut_words(fin1)
        #定义停用词
        stop = ['的','你','了','将','为','例',' ','多','再','有','是','等','天','次','让','在','我','也','就','这样','啊','和','都','《','》',',','看','!','什么','怎么','这么','很','给','没有','不是','说'
        ,'不','吗','?','!' ,'?','。' ,'...' ,'电影','主','男','女'   ]
        words_cut = []
        for word in all_words:
            if word not in stop:
                words_cut.append(word)
        word_count = pd.Series(words_cut).value_counts()
        back_ground = imread("F:\\flower.jpg")
        wc = WordCloud(
                       font_path="C:\\Windows\\Fonts\\simhei.ttf", #设置字体
                       background_color="white",  #设置词云背景颜色
                       max_words=400,  #词云允许最大词汇数
                       mask=back_ground,  #词云形状
                       max_font_size=400,   #最大字体大小
                       random_state=90  #配色方案的种数
                      )
        wc1 = wc.fit_words(word_count)  #生成词云
        plt.figure()
        plt.imshow(wc1)
        plt.axis("off")
        plt.show()
        wc.to_file("ciyun.png")
        print('succeed!\n')
app = Tk()
_input = Entry()
#_input.place(x=113, y=80)
_input.pack()
app.title("电影评论关键词生成器")
screenwidth = app.winfo_screenwidth()
screenheight = app.winfo_screenheight()
dialog_width = 400
dialog_height = 170
# 前两个参数是窗口的大小,后面两个参数是窗口的位置
app.geometry(
    "%dx%d+%d+%d" % (dialog_width, dialog_height, (screenwidth - dialog_width) / 2, (screenheight - dialog_height) / 2))
btn = Button(text='查询', command=get_data,width=10)
btn.place(x=155, y=80)
btn.pack()
app.mainloop()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99

END
学业繁重,好久没更新,后续寒假可能会更新,一起加油

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/687032
推荐阅读
相关标签