抓取豆瓣上的《长津湖》的热评,我发现了这些

3,017 阅读2分钟

大家,我是K同学啊!

近期一部《长津湖》火爆朋友圈,被各种安利,咱一个人也不想跑电影院去霸个情侣座,就老老实实分析一下影评,看看大家的“观后感”是吧~ 首先定位目标网页

movie.douban.com/subject/258…

上爬虫,抓取下面四个字段

然后使用pandas对导入数据并做简单的处理

import pandas as pd
import os

file_path = os.path.join("douban.csv")

#读取test.csv文件中的A、B列,若不设置usecols参数,默认读取全部数据。
df = pd.read_csv(open(file_path,'r',encoding='utf-8'), names=["用户名","星评","评论时间","评论"])
df.head()

star_num = df.星评.value_counts()
star_num = star_num.sort_index()
star_num
力荐        112
推荐         35
该用户未星评      2
较差         14
还行         37
Name: 星评, dtype: int64

豆瓣短评评分占比

from pyecharts.charts import Pie, Bar, Line, Page
from pyecharts import options as opts 
from pyecharts.globals import SymbolType

# 数据对
data_pair = [list(z) for z in zip([i for i in star_num.index], star_num.values.tolist())]

# 饼图
pie1 = Pie(init_opts=opts.InitOpts(width='800px', height='400px'))
pie1.add('', data_pair, radius=['35%''60%'])
pie1.set_global_opts(title_opts=opts.TitleOpts(title='豆瓣短评评分占比'), 
                     legend_opts=opts.LegendOpts(orient='vertical', pos_top='15%', pos_left='2%')
                    ) 
pie1.set_series_opts(label_opts=opts.LabelOpts(formatter='{b}:{d}%'))
pie1.render_notebook()

在这里插入图片描述

在这里插入图片描述

评论数量走势图

# 折线图
line1 = Line(init_opts=opts.InitOpts(width='800px', height='400px'))
line1.add_xaxis(comment_date.index.tolist())
line1.add_yaxis('', comment_date.values.tolist(),
                #areastyle_opts=opts.AreaStyleOpts(opacity=0.5),
                label_opts=opts.LabelOpts(is_show=False))
line1.set_global_opts(title_opts=opts.TitleOpts(title='评论数量走势图'), 
#                       toolbox_opts=opts.ToolboxOpts(),
                      visualmap_opts=opts.VisualMapOpts(max_=140))
line1.set_series_opts(linestyle_opts=opts.LineStyleOpts(width=4))
line1.render_notebook()

9月30号上映,9月29号就开始造势了,30号达到高峰,但是1号似乎势头大减啊。

词云图

正面

import jieba

def get_cut_words(content_series):
    # 读入停用词表
    stop_words = [] 
    
    with open(r"hit_stopwords.txt"'r', encoding='utf-8'as f:
        lines = f.readlines()
        for line in lines:
            stop_words.append(line.strip())

    # 添加关键词
    my_words = ['长津湖''志愿军']  
    for i in my_words:
        jieba.add_word(i) 

#     自定义停用词
    my_stop_words = ['电影',"长津湖","战争"] 
    stop_words.extend(my_stop_words)               

    # 分词
    word_num = jieba.lcut(content_series.str.cat(sep='。'), cut_all=False)

    # 条件筛选
    word_num_selected = [i for i in word_num if i not in stop_words and len(i)>=2]
    
    return word_num_selected
text1 = get_cut_words(content_series=df[(df.星评=='力荐')|(df.星评=='推荐')]['评论'])
text1[:5]
['牺牲', '冰雪', '战士', '应该', '遗忘']
import stylecloud
from IPython.display import Image # 用于在jupyter lab中显示本地图片



# 绘制词云图
stylecloud.gen_stylecloud(text=' '.join(text1), 
                          max_words=1000,
                          collocations=False,
                          font_path=r'经典综艺体简.ttf',
                          icon_name='fas fa-thumbs-up',
                          size=360,
                          output_name='豆瓣正向评分词云图.png')

Image(filename='豆瓣正向评分词云图.png'

负面

text2 = get_cut_words(content_series=df[(df.星评=='还行')|(df.星评=='较差')]['评论'])
text2[:5]
['有点', '失望', '剧情', '一如既往', '人物']
# 绘制词云图
stylecloud.gen_stylecloud(text=' '.join(text2), 
                          max_words=1000,
                          collocations=False,
                          font_path=r'经典综艺体简.ttf',
                          icon_name='fas fa-thumbs-down',
                          size=350,
                          output_name='豆瓣负向评分词云图.png')
Image(filename='豆瓣负向评分词云图.png'