共计 3888 个字符,预计需要花费 10 分钟才能阅读完成。
提醒:本文最后更新于 2019-05-12 15:04,文中所关联的信息可能已发生改变,请知悉!
前言:
纵观近几年的国产电影市场,“开心麻花“似乎已经成为了票房的保证。从《夏洛特烦恼》、《羞羞的铁拳》到最新上映的《西虹市首富》都引爆了票房。本期我们会根据从猫眼电影网爬取到的上万条评论为你解读《西虹市首富》是否值得一看。

数据爬取:
此次数据爬取我们参考了之前其他文章中对于猫眼数据的爬取方法,调用其接口,每次取出部分数据并进行去重,最终得到上万条评论,代码如下:
tomato = pd.DataFrame(columns=[\'date\',\'score\',\'city\',\'comment\',\'nick\'])for i in range(0, 1000):j = random.randint(1,1000)print(str(i) \'\' str(j))try:time.sleep(2)url= \'http://m.maoyan.com/mmdb/comments/movie/1212592.json?_v_=yes&offset=\' str(j)html = requests.get(url=url).contentdata = json.loads(html.decode(\'utf-8\'))[\'cmts\']for item in data:tomato = tomato.append({\'date\':item[\'time\'].split(\'\')[0],\'city\':item[\'cityName\'],\'score\':item[\'score\'],\'comment\':item[\'content\'],\'nick\':item[\'nick\']},ignore_index=True)tomato.to_csv(\'西虹市首富 4.csv\',index=False)except:continue
数据分析:
我们看一下所得到的数据:

首先看一下,评论分布热力图:

下面我们要看的是主要城市的评论数量与打分情况:

我们按照打分从高到底对城市进行排序:

我们把城市打分情况投射到地图中:(红色表示打分较高,蓝色表示较低)

较高区域:


看过了评分,我们看一下评论生成的词云图,以下分别是原图和据此绘制的词云图:


部分代码展示:
热力图:
tomato_com = pd.read_excel(\'西虹市首富.xlsx\')
grouped=tomato_com.groupby([\'city\'])
grouped_pct=grouped[\'score\']
#tip_pct 列
city_com = grouped_pct.agg([\'mean\',\'count\'])
city_com.reset_index(inplace=True)
city_com[\'mean\']
= round(city_com[\'mean\'],2)
data=[(city_com[\'city\'][i],city_com[\'count\'][i])
for i in range(0,
city_com.shape[0])]
geo =
Geo(\'《西虹市首富》全国热力图 \', title_color="#fff",
title_pos="center", width=1200,
height=600, background_color=\'#404a59\')
attr, value = geo.cast(data)
geo.add("", attr, value, type="heatmap", visual_range=[0,
200],visual_text_color="#fff",
symbol_size=10, is_visualmap=True,is_roam=False)
geo.render(\'西虹市首富全国热力图.html\')
折线图 柱形图组合:
city_main = city_com.sort_values(\'count\',ascending=False)[0:20]
attr = city_main[\'city\']
v1=city_main[\'count\']
v2=city_main[\'mean\']
line =
Line("主要城市评分")
line.add("城市", attr, v2, is_stack=True,xaxis_rotate=30,yaxis_min=4.2,
mark_point=[\'min\',\'max\'],xaxis_interval =0,line_color=\'lightblue\',
line_width=4,mark_point_textcolor=\'black\',mark_point_color=\'lightblue\',
is_splitline_show=False)
bar =
Bar("主要城市评论数")
bar.add("城市", attr, v1, is_stack=True,xaxis_rotate=30,yaxis_min=4.2,
xaxis_interval =0,is_splitline_show=False)
overlap =
Overlap()
# 默认不新增 x y 轴,并且 x y 轴的索引都为 0
overlap.add(bar)
overlap.add(line, yaxis_index=1, is_add_yaxis=True)
overlap.render(\'主要城市评论数_平均分.html\')
词云:
tomato_str =
\'\'.join(tomato_com[\'comment\'])
words_list =
[]
word_generator = jieba.cut_for_search(tomato_str)
for word in word_generator:
words_list.append(word)
words_list =
[k for k in words_list if len(k)>1]
back_color = imread(\'西红柿.jpg\')
# 解析该图片
wc =
WordCloud(background_color=\'white\',
# 背景颜色
max_words=200,
# 最大词数
mask=back_color,
# 以该参数值作图绘制词云,这个参数不为空时,width 和 height 会被忽略
max_font_size=300,
# 显示字体的最大值
stopwords=STOPWORDS.add(\'苟利国 \'),
# 使用内置的屏蔽词,再添加 \'苟利国 \'
font_path="C:/Windows/Fonts/STFANGSO.ttf",
random_state=42,
# 为每个词返回一个 PIL 颜色
# width=1000, # 图片的宽
# height=860 #图片的长
)
tomato_count =
Counter(words_list)
wc.generate_from_frequencies(tomato_count)
# 基于彩色图像生成相应彩色
image_colors =
ImageColorGenerator(back_color)
# 绘制词云
plt.figure()
plt.imshow(wc.recolor(color_func=image_colors))
plt.axis(\'off\')
票房预估:
最后我们来大胆预估下《西虹市首富》的票房,我们日常在工作中会选取标杆来对一些即将发生的事情进行预估。这次我们选择的标杆就是《羞羞的铁拳》:

- 均是开心麻花出品、题材相似
- 演员阵容重合度高
- 豆瓣粉丝认可程度相似(评分均为 6.9,处于喜剧片中位数水平)
- 猫眼粉丝认可程度相似(铁拳评分 9.1, 西红柿评分 9.3)
我们看一下两部影片前三天的走势:
