爬了 48048 条评论,解读 9.3 分的「毒液」是否值得一看?
(給Python開發(fā)者加星標(biāo),提升Python技能)
轉(zhuǎn)自:CSDN-Ryan
11月,由湯姆·哈迪主演的“毒液:致命守護(hù)者”在國(guó)內(nèi)上映,依托漫威的光環(huán)以及演員們精湛的演技,這部動(dòng)作科幻片在貓眼評(píng)分得到豆瓣7.4的評(píng)分,口碑和票房都高于大多數(shù)同期上映的其他影片。
所以周日的時(shí)候跟基友去電影院去看了這場(chǎng)正邪共生的電影,100多人的影院座無(wú)虛席,不過看完之后對(duì)比其他漫威作品,我倒也沒覺得有多大的驚喜,覺得貓眼上的9.3評(píng)分的感受不符。
頭部的幾條評(píng)論顯然有些夸大,那大眾對(duì)“毒液”感受是怎么呢?于是筆者動(dòng)手開始分析起來(lái)。
獲取數(shù)據(jù)
首先要獲取數(shù)據(jù),準(zhǔn)備爬取貓眼上的電影評(píng)論作為本次分析樣本,PC官網(wǎng)上只顯示了電影的10條熱門短評(píng),顯然不夠,于是準(zhǔn)備從M端抓包找到評(píng)論接口。
?
接口鏈接:
http://m.maoyan.com/mmdb/comments/movie/42964.json?v=yes&offset=15&startTime=2018-11-20%2019%3A17%3A16。
接口中對(duì)我們本次抓取主要有用的參數(shù)是offset偏移量以及日期,這兩個(gè)條件限制了抓取的條數(shù)。分析接口結(jié)果:
?
這里有用戶評(píng)論的相關(guān)數(shù)據(jù),我們選取了地理位置(用戶為授權(quán)無(wú)法獲取)、評(píng)論內(nèi)容、用戶名、評(píng)分以及評(píng)論時(shí)間的數(shù)據(jù),通過python的requests模塊開始爬取。導(dǎo)入本次爬取需要的包,開始抓取數(shù)據(jù)。
????????headers?=?{
????????????'User-Agent':?'Mozilla/5.0?(iPhone;?CPU?iPhone?OS?11_0?like?Mac?OS?X)?AppleWebKit/604.1.38?(KHTML,?like?Gecko)?Version/11.0?Mobile/15A372?Safari/604.1'}
????????html?=?requests.get(url,?headers=headers)
????????if?html.status_code?==200:
????????????return?html.content
????????else:
????????????return?none`
其次是解析Json數(shù)據(jù),每個(gè)接口有15條評(píng)論數(shù)據(jù),10條熱門評(píng)論數(shù)據(jù),我們將評(píng)論數(shù)據(jù)中用戶名、城市名、評(píng)論內(nèi)容、評(píng)分、評(píng)論時(shí)間依次解析出來(lái),并返回。
????json_data?=?json.loads(html)['cmts']
????comments?=?[]
????try:
????????for?item?in?json_data:
????????????comment?=?{
????????????????'nickName':?item['nickName'],
????????????????'cityName':?item['cityName']?if?'cityName'?in?item?else?'',
????????????????'content':?item['content'].strip().replace('\n',?''),
????????????????'score':?item['score'],
????????????????'startTime':?item['startTime']
????????????}
????????????comments.append(comment)
????????return?comments
????except?Exception?as?e:
????????print(e)`
接著我們將獲取到的數(shù)據(jù)保存到本地。此過程中,對(duì)接口url中時(shí)間的處理借鑒了其他博主的爬蟲思路,將每次爬取的15條數(shù)據(jù)取最后一條的評(píng)論時(shí)間,減去一秒(防止重復(fù)),從該時(shí)間向前獲取直到影片上映時(shí)間,獲取所有數(shù)據(jù)。
????start_time?=?datetime.now().strftime('%Y-%m-%d?%H:%M:%S')
????end_time?=?'2018-11-09?00:00:00'
????while?start_time?>?end_time:
????????url?=?'http://m.maoyan.com/mmdb/comments/movie/42964.json?_v_=yes&offset=15&startTime='?+?start_time.replace(
????????????'?',?'%20')
????????html?=?None
????????try:
????????????html?=?get_data(url)
????????except?Exception?as?e:
????????????time.sleep(0.5)
????????????html?=?get_data(url)
????????else:
????????????time.sleep(0.1)
????????comments?=parse_data(html)
????????start_time?=?comments[14]['startTime']
????????print(start_time)
????????start_time?=?datetime.strptime(start_time,?'%Y-%m-%d?%H:%M:%S')?+?timedelta(seconds=-1)
????????start_time?=?datetime.strftime(start_time,?'%Y-%m-%d?%H:%M:%S')
????????for?item?in?comments:
????????????print(item)
????????????with?open('files/comments.txt',?'a',?encoding='utf-8')as?f:
????????????????f.write(item['nickName']+','+item['cityName']?+','+item['content']+','+str(item['score'])+?item['startTime']?+?'\n')
if?__name__?==?'__main__':
????url?=?'http://m.maoyan.com/mmdb/comments/movie/42964.json?_v_=yes&offset=15&startTime=2018-11-19%2019%3A36%3A43'
????html?=?get_data(url)
????reusults?=?parse_data(html)
????save()`
最終抓取了48048條評(píng)論相關(guān)數(shù)據(jù)作為此次分析樣本。?
數(shù)據(jù)可視化
數(shù)據(jù)可視化采用了pyecharts,按照地理位置制作了毒液觀眾群的分布圖。部分代碼如下:
????attr,?value?=?geo.cast(data)
????geo.add('',?attr,?value,?visual_range=[0,?1000],
????????????visual_text_color='#fff',?symbol_size=15,
????????????is_visualmap=True,?is_piecewise=False,?visual_split_number=10)
????geo.render('觀眾位置分布-地理坐標(biāo)圖.html')
????data_top20?=?Counter(cities).most_common(20)
????bar?=?Bar('《毒液》觀眾來(lái)源排行TOP20',?'數(shù)據(jù)來(lái)源:貓眼-Ryan采集',?title_pos='center',?width=1200,?height=600)
????attr,?value?=?bar.cast(data_top20)
????bar.add('',?attr,?value,?is_visualmap=True,?visual_range=[0,?3500],?visual_text_color='#fff',?is_more_utils=True,
????????????is_label_show=True)
????bar.render('觀眾來(lái)源排行-柱狀圖.html')`
從可視化結(jié)果來(lái)看,“毒液”觀影人群以東部城市為主,觀影的top5城市為深圳、北京、上海、廣州、成都。?
觀眾地理位置分布圖
觀眾來(lái)源排行TOP20
用戶評(píng)論,詞云圖
只看觀眾分布無(wú)法判斷大家對(duì)電影的喜好,所以我把通過jieba把評(píng)論分詞,最后通過wordcloud制作詞云,作為大眾對(duì)該電影的綜合評(píng)價(jià)。
????with?open('files/comments.txt',?'r',?encoding='utf-8')as?f:
????????rows?=?f.readlines()
????????try:
????????????for?row?in?rows:
????????????????comment?=?row.split(',')[2]
????????????????if?comment?!=?'':
???????????????????comments.append(comment)
????????????????#?print(city)
????????except?Exception?as?e:
????????????print(e)
????comment_after_split?=?jieba.cut(str(comments),?cut_all=False)
????words?=?'?'.join(comment_after_split)
????#多慮沒用的停止詞
????stopwords?=?STOPWORDS.copy()
????stopwords.add('電影')
????stopwords.add('一部')
????stopwords.add('一個(gè)')
????stopwords.add('沒有')
????stopwords.add('什么')
????stopwords.add('有點(diǎn)')
????stopwords.add('感覺')
????stopwords.add('毒液')
????stopwords.add('就是')
????stopwords.add('覺得')
????bg_image?=?plt.imread('venmo1.jpg')
????wc?=?WordCloud(width=1024,?height=768,?background_color='white',?mask=bg_image,?font_path='STKAITI.TTF',
???????????????????stopwords=stopwords,?max_font_size=400,?random_state=50)
????wc.generate_from_text(words)
????plt.imshow(wc)
????plt.axis('off')
????plt.show()
`
從最終的詞云結(jié)果上來(lái)看,大多數(shù)觀眾還是對(duì)“毒液”很滿意的。
推薦閱讀
(點(diǎn)擊標(biāo)題可跳轉(zhuǎn)閱讀)
手把手教你寫網(wǎng)絡(luò)爬蟲(2):迷你爬蟲架構(gòu)
Python 爬蟲實(shí)踐:《戰(zhàn)狼2》豆瓣影評(píng)分析
Python 爬蟲抓取純靜態(tài)網(wǎng)站及其資源
覺得本文對(duì)你有幫助?請(qǐng)分享給更多人
關(guān)注「Python開發(fā)者」加星標(biāo),提升Python技能
總結(jié)
以上是生活随笔為你收集整理的爬了 48048 条评论,解读 9.3 分的「毒液」是否值得一看?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Cent OS 使用nohup 启动 S
- 下一篇: 如何取消您的Nintendo Switc