自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

《狄仁杰之四大天王》影評分析(爬蟲+詞云+熱力圖)

大數(shù)據(jù) 數(shù)據(jù)分析
作為徐老怪的忠實影迷,《狄仁杰之四大天王》肯定是要去看的,看豆瓣評分和前兩部相差不多,但其實胡蘿卜醬并不是很喜歡前兩部,所以在猶豫要不要去看,受到糖甜甜甜的《我不是藥神》推送影響,于是也簡單去分析了一下《狄仁杰》的影評。

作為徐老怪的忠實影迷,《狄仁杰之四大天王》肯定是要去看的,看豆瓣評分和前兩部相差不多,但其實胡蘿卜醬并不是很喜歡前兩部,所以在猶豫要不要去看,受到糖甜甜甜的《我不是藥神》推送影響,于是也簡單去分析了一下《狄仁杰》的影評。

[[238686]]

01 數(shù)據(jù)爬取

本來小編是打算爬取豆瓣的全部影評,但是很不幸,數(shù)據(jù)爬取到一半,我的賬號被封了(建議大家改為動態(tài)IP進行爬取,代碼可用,可加QQ群獲取),于是轉(zhuǎn)戰(zhàn)于貓眼。貓眼的短評不能直接通過源碼獲得,需要尋找他的數(shù)據(jù)接口,如下:

http://m.maoyan.com/mmdb/comments/movie/341516.json?_v_=yes&offset=1

其中341516屬于電影的專屬id,offset代表頁數(shù)且只展示1000頁的內(nèi)容,同時僅限于當(dāng)日的評論。此文只為做影評分析演示,所以小編只爬取了今日的評論,去重后僅為幾百條,建議大家多爬取今天的數(shù)據(jù),在做分析。

代碼如下:

  1. import requests 
  2. import json 
  3. import time 
  4. import random 
  5.  
  6. #下載一頁數(shù)據(jù) 
  7. def get_one_page(url): 
  8.     headers = { 
  9.     'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36' 
  10.     } 
  11.     response = requests.get(url,headers=headers) 
  12.     if response.status_code == 200:  #頁面正常響應(yīng) 
  13.         return response.text # 返回頁面源代碼 
  14.     return None 
  15.  
  16. #解析一頁數(shù)據(jù) 
  17. def parse_ono_page(html): 
  18.     data = json.loads(html)['cmts'] #評論以json形式存儲,故以json形式截取 
  19.     for item in data: 
  20.         yield{ #該方法返回一個字典 
  21.             'comment':item['content'], 
  22.             'date':item['time'].split(' ')[0], 
  23.             'rate':item['score'], 
  24.             'city':item['cityName'], 
  25.             'nickname':item['nickName'
  26.         } 
  27.  
  28. #保存數(shù)據(jù)到文本文檔 
  29. def save_to_txt(): 
  30.     for i in range(1, 1001): 
  31.         url='http://m.maoyan.com/mmdb/comments/movie/341516.json?_v_=yes&offset=' + str(i) 
  32.         html = get_one_page(url) 
  33.         print('正在保存第%d頁.'% i) 
  34.         for item in parse_ono_page(html): 
  35.             with open('狄仁杰.txt','a',encoding='utf-8'as f: 
  36.                 f.write(item['date'] + ',' + item['nickname'] + ',' + item['city'] + ',' +str(item['rate'])+','+item['comment']+'\n'
  37.         #反爬 
  38.         time.sleep(5 + float(random.randint(1,100)) /20)  
  39.  
  40. # 獲取的評論可能有重復(fù),為了最終統(tǒng)計的真實性,需做去重處理 
  41. def delete_repeat(old,new): 
  42.     oldfile = open(old,'r',encoding='UTF-8'
  43.     newfile = open(new,'w',encoding='UTF-8'
  44.     content_list = oldfile.readlines() #讀取的數(shù)據(jù)集 
  45.     content_alreadly_ditinct = [] #存儲不重復(fù)的評論數(shù)據(jù) 
  46.     for line in content_list: 
  47.         if line not in content_alreadly_ditinct: #評論不重復(fù) 
  48.             newfile.write(line+'\n'
  49.             content_alreadly_ditinct.append(line) 
  50.  
  51. if __name__ =='__main__'
  52.     save_to_txt() 
  53.     delete_repeat(r'狄仁杰.txt', r'狄仁杰_new.txt'

爬取數(shù)據(jù)如下:

《狄仁杰之四大天王》影評分析(爬蟲+詞云+熱力圖)

02 數(shù)據(jù)分析

直觀來看,有用的數(shù)據(jù)僅為城市,評分和評論。我們分別采用熱力圖,柱狀圖和云圖來進行可視化分析。

觀影者分布熱力圖

根據(jù)觀影者的城市,我們畫出了觀影者分布情況,如下圖:

《狄仁杰之四大天王》影評分析(爬蟲+詞云+熱力圖)

 

可明顯發(fā)現(xiàn)東部地區(qū),尤其是北京、上海、廣東,成都等城市更為突出,當(dāng)然,這和經(jīng)濟發(fā)展有著重要的聯(lián)系,也符合我們的常規(guī)想法。

代碼如下:

  1. from wordcloud import WordCloud, STOPWORDS 
  2. import pandas as pd 
  3. import jieba 
  4. import matplotlib.pyplot as plt 
  5. import seaborn as sns 
  6. from pyecharts import Geo 
  7.  
  8. f = open('狄仁杰_new.txt',encoding='UTF-8'
  9. data = pd.read_csv(f,sep=',',header=None,encoding='UTF-8',names=['date','nickname','city','rate','comment']) 
  10.  
  11. city = data.groupby(['city']) 
  12. rate_group = city['rate'
  13. city_com = city['city'].agg(['count']) 
  14. city_com.reset_index(inplace=True
  15. data_map = [(city_com['city'][i],city_com['count'][i]) for i in range(0,city_com.shape[0])] 
  16. geo = Geo("狄仁杰",title_color="#fff",title_pos="center",width=1200, 
  17.           height=600,background_color="#404a59"
  18.  
  19. while True:  
  20.     try: 
  21.         attr, val = geo.cast(data_map) 
  22.         geo.add("", attr, val, visual_range=[0, 50], visual_text_color="#fff", is_geo_effect_show=False
  23.                 is_piecewise=True, visual_split_number=6, symbol_size=15, is_visualmap=True
  24.  
  25.     except ValueError as e: 
  26.         e = str(e) 
  27.         e = e.split("No coordinate is specified for ")[1]  # 獲取不支持的城市名稱 
  28.         for i in range(0,len(data_map)): 
  29.             if e in data_map[i]: 
  30.                 data_map.pop(i) 
  31.                 break 
  32.     else
  33.         break 
  34. geo.render('狄仁杰.html'

評分柱狀圖

相較于豆瓣的評分機制,在貓眼的評分機制允許給出半星的評價,于是通過簡單的柱狀圖來大概看一下評分情況。

《狄仁杰之四大天王》影評分析(爬蟲+詞云+熱力圖)

 

大部分的評分都高于3分,尤其是給出滿分的最多,只有少數(shù)的觀影者給出了低分,看來徐克這部電影的口碑還行,至少在及格線以上。那大家觀完影,最直觀的感受是什么呢,接下來就通過短評分析來看看。

代碼如下:

  1. #評分分析 
  2. rate = data['rate'].value_counts() 
  3.  
  4. sns.set_style("darkgrid"
  5. bar_plot = sns.barplot(x=rate.index,y=(rate.values/sum(rate)),palette="muted"
  6. plt.xticks(rotation=90) 
  7. plt.show() 

短評詞云圖

為了分析短評,我們采用jieba對短評進行了分詞,然后做出詞云圖。

背景圖:

[[238688]]

 

詞云圖:

 

[[238689]]

和前兩部一樣,最突出的是特效,劇情和徐老怪,看來在這部電影里面,徐克肯定也將特效做的很棒,個人風(fēng)格也肯定展示的***。同時,也能發(fā)現(xiàn)好看,不錯,推薦也是觀影者的主要感受,看來還是蠻值得去看的。

代碼如下:

  1. #分詞 
  2. comment = jieba.cut(str(data["comment"]),cut_all=False
  3. wl_space_split= " ".join(comment) 
  4. #導(dǎo)入背景圖 
  5. backgroud_Image = plt.imread('xuke.jpg'
  6. stopwords = STOPWORDS.copy() 
  7. print(" STOPWORDS.copy()",help(STOPWORDS.copy())) 
  8. #可以自行加多個屏蔽詞,也可直接下載停用詞表格 
  9. stopwords.add("電影"
  10. stopwords.add("一部"
  11. stopwords.add("一個"
  12. stopwords.add("沒有"
  13. stopwords.add("什么"
  14. stopwords.add("有點"
  15. stopwords.add("這部"
  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. stopwords.add("為了"
  27. stopwords.add("比較"
  28. print (stopwords) 
  29. #設(shè)置詞云參數(shù) 
  30. #參數(shù)分別是指定字體/背景顏色/***的詞的大小,使用給定圖作為背景形狀 
  31. wc =WordCloud(width=1024,height=768,background_color='white'
  32.               mask = backgroud_Image,font_path='C:/Windows/Fonts/simkai.ttf'
  33.               stopwords=stopwords,max_font_size=400, 
  34.               random_state=50) 
  35. #將分詞后數(shù)據(jù)傳入云圖 
  36. wc.generate_from_text(wl_space_split) 
  37. plt.imshow(wc) 
  38. plt.axis('off')#不顯示坐標(biāo)軸 
  39. plt.show() 
  40. #保存結(jié)果到本地 
  41. wc.to_file(r'xuke_wordcloud.jpg'

03 結(jié)語

本文只是做了簡單的數(shù)據(jù)爬取和分析,主要是用以學(xué)習(xí)如何進行分詞,詞云和熱力圖,建議大家可以基于此進行練習(xí)。(胡蘿卜醬要抽空去看狄仁杰了,開熏)

責(zé)任編輯:未麗燕 來源: 經(jīng)管人學(xué)數(shù)據(jù)分析
相關(guān)推薦

2010-07-19 10:05:07

jQuery

2018-03-20 14:40:58

Python爬蟲豆瓣影評

2014-03-27 15:57:45

Android組件Activity

2012-05-10 18:10:35

2012-02-08 13:52:30

云計算

2019-03-01 09:10:59

數(shù)據(jù)保護備份數(shù)據(jù)管理

2011-10-21 13:59:48

Java云計算

2012-07-19 09:15:43

CloudStackEucalyptusvCloud Dire

2019-06-14 16:25:14

關(guān)鍵詞SAP Concur企業(yè)

2016-12-09 09:23:50

android組件Service

2023-08-07 07:16:33

AMDNavi 31Navi 31 XL

2016-03-30 11:51:55

2016-03-17 17:35:15

云容器虛擬化管理Docker

2011-06-10 09:44:15

iCloud蘋果

2015-07-17 09:50:16

Carthage優(yōu)劣比較

2012-03-26 21:52:07

平臺

2021-03-08 08:21:19

詞云數(shù)據(jù)可視化大數(shù)據(jù)

2010-07-30 09:27:48

云計算IT基礎(chǔ)設(shè)施CIO

2012-09-12 10:07:20

云計算云服務(wù)

2013-07-23 10:13:17

點贊
收藏

51CTO技術(shù)棧公眾號