火爆全網(wǎng)的《魷魚游戲》,今天用 Python 分析一波影評
Hello,各位讀者朋友們好啊,我是小張~
這不國慶嘛,就把最近很火的一個韓劇《魷魚游戲》刷了下,這部劇整體劇情來說還是非常不錯的,很值得一看,
作為一個技術(shù)博主,當(dāng)然不能在這兒介紹這部劇的影評,畢竟自己在這方面不是專業(yè)的,最關(guān)鍵還是自己也寫不出來,,,
本文呢,主要是爬取《魷魚游戲》在豆瓣上的一些影評,對數(shù)據(jù)做一些簡單的分析,用數(shù)據(jù)的角度重新審視下這部劇
技術(shù)工具
在正文開始之前,先介紹下本篇文章中用到的技術(shù)棧和工具。本文中涉及到的全部源碼數(shù)據(jù),在公眾號【程序員大飛】后臺回復(fù)關(guān)鍵字 211003 即可獲取。
本文用到的技術(shù)棧和工具如下,歸結(jié)為四個方面;
語言:Python,Vue ,javascript;
存儲:MongoDB;
庫:echarts ,Pymongo,WordArt...
軟件:Photoshop;
數(shù)據(jù)采集
本次數(shù)據(jù)采集的目標(biāo)網(wǎng)站為 豆瓣 ,但自己的賬號之前被封,所以只能采集到大概二百來條數(shù)據(jù),豆瓣有相應(yīng)的反爬機(jī)制,瀏覽10頁以上的評論需要用戶登錄才能進(jìn)行下一步操作
至于為啥賬號被封,是因為之前自己學(xué)爬蟲時不知道在哪里搞的【豆瓣模擬登錄】代碼,當(dāng)時不知道代碼有沒有問題,愣頭青直接用自己的號試了下,誰知道剛試完就被封了,而且還是永久的那種
[圖片上傳失敗...(image-4108c6-1633427749655)]
<figcaption style="margin-top: 5px; text-align: center; color: #888; font-size: 14px;">image-20211002125521303</figcaption>
<center style="color:rgba(0,0,0,0.6);" data-tool="mdnice編輯器">圖1</center>
在這里也給大家提個醒在以后做爬蟲時,模擬登錄時盡量用一些測試賬號,能不用自己的號就別用,
這次數(shù)據(jù)采集也比較簡單,就是更改圖2 中 url 上的 start 參數(shù),以 offset 為 20 的規(guī)則 作為下一頁 url 的拼接;
[圖片上傳失敗...(image-3d7fe9-1633427749655)]
<figcaption style="margin-top: 5px; text-align: center; color: #888; font-size: 14px;">image-20211002130109452</figcaption>
<center style="color:rgba(0,0,0,0.6);" data-tool="mdnice編輯器">圖2</center>
拿到 請求連接之后,用 requests 的 get 請求,再對獲取到的 html 數(shù)據(jù)做個解析,就能獲取到我們需要的數(shù)據(jù)了;采集核心代碼貼在下方
<pre class="custom" data-tool="mdnice編輯器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;">`for offset in range(0,220,20):
url = "https://movie.douban.com/subject/34812928/comments?start={}&limit=20&status=P&sort=new_score".format(offset)
res = requests.get(url,headers= headers)
# print(res.text)
soup = BeautifulSoup(res.text,'lxml')
time.sleep(2)
for comment_item in soup.select("#comments > .comment-item"):
try:
data_item = []
avatar = comment_item.select(".avatar a img")[0].get("src")
name = comment_item.select(".comment h3 .comment-info a")[0]
rate = comment_item.select(".comment h3 .comment-info span:nth-child(3)")[0]
date = comment_item.select(".comment h3 .comment-info span:nth-child(4)")[0]
comment = comment_item.select(".comment .comment-content span")[0]
# comment_item.get("div img").ge
data_item.append(avatar)
data_item.append(str(name.string).strip("\t"))
data_item.append(str(rate.get("class")[0]).strip("allstar").strip('\t').strip("\n"))
data_item.append(str(date.string).replace('\n','').strip('\t'))
data_item.append(str(comment.string).strip("\t").strip("\n"))
data_json ={
'avatar':avatar,
'name': str(name.string).strip("\t"),
'rate': str(rate.get("class")[0]).strip("allstar").strip('\t').strip("\n"),
'date' : str(date.string).replace('\n','').replace('\t','').strip(' '),
'comment': str(comment.string).strip("\t").strip("\n")
}
if not (collection.find_one({'avatar':avatar})):
print("data _json is {}".format(data_json))
collection.insert_one(data_json)
f.write('\t'.join(data_item))
f.write("\n")
except Exception as e:
print(e)
continue` </pre>
豆瓣爬取時需要記得加上 cookie 和 User-Agent,否則不會有數(shù)據(jù)為空,
為了后面數(shù)據(jù)可視化提取方便,本文用的是 Mongodb 作為數(shù)據(jù)存儲,共有211 條數(shù)據(jù),主要采集的數(shù)據(jù)字段為 avatar,name、rate、date、comment,分別表示用戶頭像、用戶名字、星級、日期,評論;結(jié)果見圖3;
[圖片上傳失敗...(image-4390f7-1633427749654)]
<figcaption style="margin-top: 5px; text-align: center; color: #888; font-size: 14px;">image-20211002130736309</figcaption>
<center style="color:rgba(0,0,0,0.6);" data-tool="mdnice編輯器">圖3</center>
關(guān)于 Python 怎么使用 MongoDB,可以參考舊聞 【】
數(shù)據(jù)可視化
可視化部分之前打算用 Python + Pyecharts 來實現(xiàn),但 Python 圖表中的交互效果不是很好,索性就直接用原生 Echarts + Vue 組合來實現(xiàn),而且,這樣的話,將所有圖表放在一個網(wǎng)頁中也比較方便
首先是對評論時間與評論數(shù)量做了一個圖表預(yù)覽,根據(jù)這些數(shù)據(jù)的評論時間作為一個散點圖分布,看一下用戶評論主要的時間分布
[圖片上傳失敗...(image-30db4c-1633427749654)]
<figcaption style="margin-top: 5px; text-align: center; color: #888; font-size: 14px;">動畫11</figcaption>
<center style="color:rgba(0,0,0,0.6);" data-tool="mdnice編輯器">圖4</center>
圖4中點的大小和顏色代表當(dāng)天評論數(shù)量,而評論數(shù)量也可以側(cè)面反應(yīng)該劇當(dāng)天的熱度。
可以 了解到,《魷魚游戲》影評從 9 月17 日開始增長,在 20 號數(shù)量達(dá)到頂峰,21 日回落;在21日-29日評論數(shù)量來回震蕩,相差不大;
直到國慶 10月1日最少,猜測可能是一方面是國慶假期大家都出去玩的緣故,另一方面是隨著時間推移,這個劇的熱度也就降下來了
為了了解大家對《魷魚游戲》的評價,我對這二百條數(shù)據(jù)對這個劇的【評分星級】繪制了一個餅圖,最終效果見 圖5
[圖片上傳失敗...(image-8c5e79-1633427749654)]
<figcaption style="margin-top: 5px; text-align: center; color: #888; font-size: 14px;">動畫2</figcaption>
<center style="color:rgba(0,0,0,0.6);" data-tool="mdnice編輯器">圖5</center>
說實話圖5 的結(jié)果讓我有些意外,至少對于我而言這部劇質(zhì)量說實話還是蠻高的,繪圖之前以為【五星】的占比應(yīng)該是最大的,其次是【四星】,再然后是【三星】;
現(xiàn)在【三星】和【五星】的占比恰恰相反,猜測可能是這部劇的情節(jié)比較殘忍,會引起人的不適,所以高分占比不高;
為了方便,最后我將上面兩張圖表放置在一個網(wǎng)頁上,效果見圖6 和 圖7 兩種不同布局
垂直布局
[圖片上傳失敗...(image-e4ace6-1633427749654)]
<figcaption style="margin-top: 5px; text-align: center; color: #888; font-size: 14px;">動畫3</figcaption>
<center style="color:rgba(0,0,0,0.6);" data-tool="mdnice編輯器">圖6</center>
水平布局
[圖片上傳失敗...(image-f187d6-1633427749654)]
<figcaption style="margin-top: 5px; text-align: center; color: #888; font-size: 14px;">動畫4</figcaption>
<center style="color:rgba(0,0,0,0.6);" data-tool="mdnice編輯器">圖7</center>
詞云可視化
本次采集的數(shù)據(jù)信息有限能分析的數(shù)據(jù)維度不多,關(guān)于數(shù)據(jù)圖表方面的分析基本就到這里了,下面是對采集到的評論做了幾張詞云圖
[圖片上傳失敗...(image-3df2f3-1633427749654)]
<figcaption style="margin-top: 5px; text-align: center; color: #888; font-size: 14px;">mask1_workArt</figcaption>
<center style="color:rgba(0,0,0,0.6);" data-tool="mdnice編輯器">圖8</center>
從圖8來看,去除現(xiàn)實中常用到的還是、就是等口頭語,人性 是影評中頻率最高的一個詞,而這個詞確實符合《魷魚游戲》這部劇的主題,從第一集開始到結(jié)束都是在刨析人性,賭徒們的”貪婪、賭性成癮“,貴賓們的”弱肉強(qiáng)食“
[圖片上傳失敗...(image-bd3818-1633427749654)]
<figcaption style="margin-top: 5px; text-align: center; color: #888; font-size: 14px;">mask1_workArt1</figcaption>
<center style="color:rgba(0,0,0,0.6);" data-tool="mdnice編輯器">圖9</center>
對比上張詞云圖,圖9凸顯的信息相對就多了些,例如韓國、人設(shè)、刺激、劇情、賭博默示錄、題材等都與劇情有關(guān),除了這幾個信息之外,李政宰、孔劉、李秉憲 等幾個主演也被提到
最后,我將采集到的用戶頭像做了兩張圖片墻作為文章的結(jié)尾
[圖片上傳失敗...(image-3877ae-1633427749654)]
<figcaption style="margin-top: 5px; text-align: center; color: #888; font-size: 14px;">mask1_wall</figcaption>
<center style="color:rgba(0,0,0,0.6);" data-tool="mdnice編輯器">圖10</center>
[圖片上傳失敗...(image-9bb38a-1633427749654)]
<figcaption style="margin-top: 5px; text-align: center; color: #888; font-size: 14px;">mask2_wall</figcaption>
<center style="color:rgba(0,0,0,0.6);" data-tool="mdnice編輯器">圖11</center>
圖10,圖11 照片墻的輪廓采用的是劇中的兩個人物截圖,一個是123木頭人 ,另外一個是男一在玩游戲二的一個鏡頭:
[圖片上傳失敗...(image-8bbefc-1633427749654)]
<figcaption style="margin-top: 5px; text-align: center; color: #888; font-size: 14px;">image-20211002171542736</figcaption>
關(guān)于照片墻制作方法,可參考舊聞:
小結(jié)
本文中涉及到的全部源碼和資料獲取方式:關(guān)注微信公號:【程序員大飛】,在后臺回復(fù)關(guān)鍵字 211003 即可獲取,
好了,以上就是本篇文章的全部內(nèi)容了,本文分析到的東西并不多,主要是介紹了 Python 在數(shù)據(jù)采集和可視化方面的一些應(yīng)用。
如果內(nèi)容對你有所幫助的話,希望給文章 點個贊 鼓勵一下我,當(dāng)然也更歡迎讀者朋友們將文章 分享 給更多的人!
最后感謝大家的閱讀,我們下期見~