爬了 48048 条评论,解读 9.3 分的「毒液」是否值得一看?

论坛 期权论坛 期权     
Python开发者   2018-11-28 16:56   2657   0
(给Python开发者加星标,提升Python技能)
转自:CSDN-Ryan
11月,由汤姆·哈迪主演的“毒液:致命守护者”在国内上映,依托漫威的光环以及演员们精湛的演技,这部动作科幻片在猫眼评分得到豆瓣7.4的评分,口碑和票房都高于大多数同期上映的其他影片。

所以周日的时候跟基友去电影院去看了这场正邪共生的电影,100多人的影院座无虚席,不过看完之后对比其他漫威作品,我倒也没觉得有多大的惊喜,觉得猫眼上的9.3评分的感受不符。

头部的几条评论显然有些夸大,那大众对“毒液”感受是怎么呢?于是笔者动手开始分析起来。

获取数据


首先要获取数据,准备爬取猫眼上的电影评论作为本次分析样本,PC官网上只显示了电影的10条热门短评,显然不够,于是准备从M端抓包找到评论接口。



接口链接:
http://m.maoyan.com/mmdb/comments/movie/42964.json?v=yes&offset=15&startTime=2018-11-20%2019%3A17%3A16。
接口中对我们本次抓取主要有用的参数是offset偏移量以及日期,这两个条件限制了抓取的条数。分析接口结果:



这里有用户评论的相关数据,我们选取了地理位置(用户为授权无法获取)、评论内容、用户名、评分以及评论时间的数据,通过python的requests模块开始爬取。导入本次爬取需要的包,开始抓取数据。
  1. `defget_data(url):
  2. headers={
  3. 'User-Agent':'Mozilla/5.0(iPhone;CPUiPhoneOS11_0likeMacOSX)AppleWebKit/604.1.38(KHTML,likeGecko)Version/11.0Mobile/15A372Safari/604.1'}
  4. html=requests.get(url,headers=headers)
  5. ifhtml.status_code==200:
  6. returnhtml.content
  7. else:
  8. returnnone`
复制代码
其次是解析Json数据,每个接口有15条评论数据,10条热门评论数据,我们将评论数据中用户名、城市名、评论内容、评分、评论时间依次解析出来,并返回。
  1. `defparse_data(html):
  2. json_data=json.loads(html)['cmts']
  3. comments=[]
  4. try:
  5. foriteminjson_data:
  6. comment={
  7. 'nickName':item['nickName'],
  8. 'cityName':item['cityName']if'cityName'initemelse'',
  9. 'content':item['content'].strip().replace('\n',''),
  10. 'score':item['score'],
  11. 'startTime':item['startTime']
  12. }
  13. comments.append(comment)
  14. returncomments
  15. exceptExceptionase:
  16. print(e)`
复制代码
接着我们将获取到的数据保存到本地。此过程中,对接口url中时间的处理借鉴了其他博主的爬虫思路,将每次爬取的15条数据取最后一条的评论时间,减去一秒(防止重复),从该时间向前获取直到影片上映时间,获取所有数据。
  1. `defsave():
  2. start_time=datetime.now().strftime('%Y-%m-%d%H:%M:%S')
  3. end_time='2018-11-0900:00:00'
  4. whilestart_time>end_time:
  5. url='http://m.maoyan.com/mmdb/comments/movie/42964.json?_v_=yes&offset=15&startTime='+start_time.replace(
  6. '','%20')
  7. html=None
  8. try:
  9. html=get_data(url)
  10. exceptExceptionase:
  11. time.sleep(0.5)
  12. html=get_data(url)
  13. else:
  14. time.sleep(0.1)
  15. comments=parse_data(html)
  16. start_time=comments[14]['startTime']
  17. print(start_time)
  18. start_time=datetime.strptime(start_time,'%Y-%m-%d%H:%M:%S')+timedelta(seconds=-1)
  19. start_time=datetime.strftime(start_time,'%Y-%m-%d%H:%M:%S')
  20. foritemincomments:
  21. print(item)
  22. withopen('files/comments.txt','a',encoding='utf-8')asf:
  23. f.write(item['nickName']+','+item['cityName']+','+item['content']+','+str(item['score'])+item['startTime']+'\n')
  24. if__name__=='__main__':
  25. url='http://m.maoyan.com/mmdb/comments/movie/42964.json?_v_=yes&offset=15&startTime=2018-11-19%2019%3A36%3A43'
  26. html=get_data(url)
  27. reusults=parse_data(html)
  28. save()`
复制代码
最终抓取了48048条评论相关数据作为此次分析样本。


数据可视化

数据可视化采用了pyecharts,按照地理位置制作了毒液观众群的分布图。部分代码如下:
  1. `geo=Geo('《毒液》观众位置分布','数据来源:猫眼-Ryan采集',**style.init_style)
  2. attr,value=geo.cast(data)
  3. geo.add('',attr,value,visual_range=[0,1000],
  4. visual_text_color='#fff',symbol_size=15,
  5. is_visualmap=True,is_piecewise=False,visual_split_number=10)
  6. geo.render('观众位置分布-地理坐标图.html')
  7. data_top20=Counter(cities).most_common(20)
  8. bar=Bar('《毒液》观众来源排行TOP20','数据来源:猫眼-Ryan采集',title_pos='center',width=1200,height=600)
  9. attr,value=bar.cast(data_top20)
  10. bar.add('',attr,value,is_visualmap=True,visual_range=[0,3500],visual_text_color='#fff',is_more_utils=True,
  11. is_label_show=True)
  12. bar.render('观众来源排行-柱状图.html')`
复制代码
从可视化结果来看,“毒液”观影人群以东部城市为主,观影的top5城市为深圳、北京、上海、广州、成都。


观众地理位置分布图



观众来源排行TOP20

用户评论,词云图


只看观众分布无法判断大家对电影的喜好,所以我把通过jieba把评论分词,最后通过wordcloud制作词云,作为大众对该电影的综合评价。
  1. `comments=[]
  2. withopen('files/comments.txt','r',encoding='utf-8')asf:
  3. rows=f.readlines()
  4. try:
  5. forrowinrows:
  6. comment=row.split(',')[2]
  7. ifcomment!='':
  8. comments.append(comment)
  9. #print(city)
  10. exceptExceptionase:
  11. print(e)
  12. comment_after_split=jieba.cut(str(comments),cut_all=False)
  13. words=''.join(comment_after_split)
  14. #多虑没用的停止词
  15. stopwords=STOPWORDS.copy()
  16. stopwords.add('电影')
  17. stopwords.add('一部')
  18. stopwords.add('一个')
  19. stopwords.add('没有')
  20. stopwords.add('什么')
  21. stopwords.add('有点')
  22. stopwords.add('感觉')
  23. stopwords.add('毒液')
  24. stopwords.add('就是')
  25. stopwords.add('觉得')
  26. bg_image=plt.imread('venmo1.jpg')
  27. wc=WordCloud(width=1024,height=768,background_color='white',mask=bg_image,font_path='STKAITI.TTF',
  28. stopwords=stopwords,max_font_size=400,random_state=50)
  29. wc.generate_from_text(words)
  30. plt.imshow(wc)
  31. plt.axis('off')
  32. plt.show()
  33. `
复制代码
从最终的词云结果上来看,大多数观众还是对“毒液”很满意的。





推荐阅读
(点击标题可跳转阅读)
手把手教你写网络爬虫(2):迷你爬虫架构
Python 爬虫实践:《战狼2》豆瓣影评分析
Python 爬虫抓取纯静态网站及其资源


觉得本文对你有帮助?请分享给更多人
关注「Python开发者」加星标,提升Python技能

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:835
帖子:167
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP