成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

python爬蟲(chóng)實(shí)戰(zhàn)一:分析豆瓣中最新電影的影評(píng)

BearyChat / 1724人閱讀

摘要:準(zhǔn)備把豆瓣上對(duì)它的影評(píng)短評(píng)做一個(gè)分析。這樣就得到了最新電影的信息了。例如戰(zhàn)狼的短評(píng)網(wǎng)址為其中就是電影的,表示評(píng)論的第條評(píng)論。如下圖所示好的,至此我們已經(jīng)爬取了豆瓣最近播放電影的評(píng)論數(shù)據(jù),接下來(lái)就要對(duì)數(shù)據(jù)進(jìn)行清洗和詞云顯示了。

簡(jiǎn)介

剛接觸python不久,做一個(gè)小項(xiàng)目來(lái)練練手。前幾天看了《戰(zhàn)狼2》,發(fā)現(xiàn)它在最新上映的電影里面是排行第一的,如下圖所示。準(zhǔn)備把豆瓣上對(duì)它的影評(píng)(短評(píng))做一個(gè)分析。

目標(biāo)總覽

主要做了三件事:

抓取網(wǎng)頁(yè)數(shù)據(jù)

清理數(shù)據(jù)

用詞云進(jìn)行展示

使用的python版本是3.5.
運(yùn)行環(huán)境:jupyer notebook,如在其他環(huán)境下運(yùn)行報(bào)錯(cuò)了,請(qǐng)查看評(píng)論區(qū)的討論,里面有一些解決辦法。

一、抓取網(wǎng)頁(yè)數(shù)據(jù)

第一步要對(duì)網(wǎng)頁(yè)進(jìn)行訪問(wèn),python中使用的是urllib庫(kù)。代碼如下:

from urllib import request
resp = request.urlopen("https://movie.douban.com/nowplaying/hangzhou/")
html_data = resp.read().decode("utf-8")

其中https://movie.douban.com/nowp...,可以在瀏覽器中輸入該網(wǎng)址進(jìn)行查看。
html_data是字符串類型的變量,里面存放了網(wǎng)頁(yè)的html代碼。
輸入print(html_data)可以查看,如下圖所示:

第二步,需要對(duì)得到的html代碼進(jìn)行解析,從里面提取我們需要的數(shù)據(jù)。
在python中使用BeautifulSoup庫(kù)進(jìn)行html代碼的解析。
(注:如果沒(méi)有安裝此庫(kù),則使用pip install BeautifulSoup進(jìn)行安裝即可?。?br>BeautifulSoup使用的格式如下:

BeautifulSoup(html,"html.parser")

第一個(gè)參數(shù)為需要提取數(shù)據(jù)的html,第二個(gè)參數(shù)是指定解析器,然后使用find_all()讀取html標(biāo)簽中的內(nèi)容。

但是html中有這么多的標(biāo)簽,該讀取哪些標(biāo)簽?zāi)??其?shí),最簡(jiǎn)單的辦法是可以打開(kāi)我們爬取網(wǎng)頁(yè)的html代碼,然后查看我們需要的數(shù)據(jù)在哪個(gè)html標(biāo)簽里面,再進(jìn)行讀取就可以了。如下圖所示:

從上圖中可以看出在div id="nowplaying"標(biāo)簽開(kāi)始是我們想要的數(shù)據(jù),里面有電影的名稱、評(píng)分、主演等信息。所以相應(yīng)的代碼編寫(xiě)如下:

from bs4 import BeautifulSoup as bs
soup = bs(html_data, "html.parser")    
nowplaying_movie = soup.find_all("div", id="nowplaying")
nowplaying_movie_list = nowplaying_movie[0].find_all("li", class_="list-item") 

其中nowplaying_movie_list 是一個(gè)列表,可以用print(nowplaying_movie_list[0])查看里面的內(nèi)容,如下圖所示:

在上圖中可以看到data-subject屬性里面放了電影的id號(hào)碼,而在img標(biāo)簽的alt屬性里面放了電影的名字,因此我們就通過(guò)這兩個(gè)屬性來(lái)得到電影的id和名稱。(注:打開(kāi)電影短評(píng)的網(wǎng)頁(yè)時(shí)需要用到電影的id,所以需要對(duì)它進(jìn)行解析),編寫(xiě)代碼如下:

nowplaying_list = [] 
for item in nowplaying_movie_list:        
        nowplaying_dict = {}        
        nowplaying_dict["id"] = item["data-subject"]       
        for tag_img_item in item.find_all("img"):            
            nowplaying_dict["name"] = tag_img_item["alt"]            
            nowplaying_list.append(nowplaying_dict)  

其中列表nowplaying_list中就存放了最新電影的id和名稱,可以使用print(nowplaying_list)進(jìn)行查看,如下圖所示:

可以看到和豆瓣網(wǎng)址上面是匹配的。這樣就得到了最新電影的信息了。接下來(lái)就要進(jìn)行對(duì)最新電影短評(píng)進(jìn)行分析了。例如《戰(zhàn)狼2》的短評(píng)網(wǎng)址為:https://movie.douban.com/subject/26363254/comments?start=0&limit=20
其中26363254就是電影的id,start=0表示評(píng)論的第0條評(píng)論。

接下來(lái)接對(duì)該網(wǎng)址進(jìn)行解析了。打開(kāi)上圖中的短評(píng)頁(yè)面的html代碼,我們發(fā)現(xiàn)關(guān)于評(píng)論的數(shù)據(jù)是在div標(biāo)簽的comment屬性下面,如下圖所示:

因此對(duì)此標(biāo)簽進(jìn)行解析,代碼如下:

requrl = "https://movie.douban.com/subject/" + nowplaying_list[0]["id"] + "/comments" +"?" +"start=0" + "&limit=20" 
resp = request.urlopen(requrl) 
html_data = resp.read().decode("utf-8") 
soup = bs(html_data, "html.parser") 
comment_div_lits = soup.find_all("div", class_="comment") 

此時(shí)在comment_div_lits 列表中存放的就是div標(biāo)簽和comment屬性下面的html代碼了。在上圖中還可以發(fā)現(xiàn)在p標(biāo)簽下面存放了網(wǎng)友對(duì)電影的評(píng)論,如下圖所示:

因此對(duì)comment_div_lits 代碼中的html代碼繼續(xù)進(jìn)行解析,代碼如下:

eachCommentList = []; 
for item in comment_div_lits: 
        if item.find_all("p")[0].string is not None:     
            eachCommentList.append(item.find_all("p")[0].string)

使用print(eachCommentList)查看eachCommentList列表中的內(nèi)容,可以看到里面存里我們想要的影評(píng)。如下圖所示:

好的,至此我們已經(jīng)爬取了豆瓣最近播放電影的評(píng)論數(shù)據(jù),接下來(lái)就要對(duì)數(shù)據(jù)進(jìn)行清洗和詞云顯示了。

二、數(shù)據(jù)清洗

為了方便進(jìn)行數(shù)據(jù)進(jìn)行清洗,我們將列表中的數(shù)據(jù)放在一個(gè)字符串?dāng)?shù)組中,代碼如下:

comments = ""
for k in range(len(eachCommentList)):
    comments = comments + (str(eachCommentList[k])).strip()

使用print(comments)進(jìn)行查看,如下圖所示:

可以看到所有的評(píng)論已經(jīng)變成一個(gè)字符串了,但是我們發(fā)現(xiàn)評(píng)論中還有不少的標(biāo)點(diǎn)符號(hào)等。這些符號(hào)對(duì)我們進(jìn)行詞頻統(tǒng)計(jì)時(shí)根本沒(méi)有用,因此要將它們清除。所用的方法是正則表達(dá)式。python中正則表達(dá)式是通過(guò)re模塊來(lái)實(shí)現(xiàn)的。代碼如下:

import re

pattern = re.compile(r"[u4e00-u9fa5]+")
filterdata = re.findall(pattern, comments)
cleaned_comments = "".join(filterdata)

繼續(xù)使用print(cleaned_comments)語(yǔ)句進(jìn)行查看,如下圖所示:


我們可以看到此時(shí)評(píng)論數(shù)據(jù)中已經(jīng)沒(méi)有那些標(biāo)點(diǎn)符號(hào)了,數(shù)據(jù)變得“干凈”了很多。

因此要進(jìn)行詞頻統(tǒng)計(jì),所以先要進(jìn)行中文分詞操作。在這里我使用的是結(jié)巴分詞。如果沒(méi)有安裝結(jié)巴分詞,可以在控制臺(tái)使用pip install jieba進(jìn)行安裝。(注:可以使用pip list查看是否安裝了這些庫(kù))。代碼如下所示:

import jieba    #分詞包
import pandas as pd  

segment = jieba.lcut(cleaned_comments)
words_df=pd.DataFrame({"segment":segment})

因?yàn)榻Y(jié)巴分詞要用到pandas,所以我們這里加載了pandas包。可以使用words_df.head()查看分詞之后的結(jié)果,如下圖所示:

從上圖可以看到我們的數(shù)據(jù)中有“看”、“太”、“的”等虛詞(停用詞),而這些詞在任何場(chǎng)景中都是高頻詞,并且沒(méi)有實(shí)際的含義,所以我們要將他們清除。

我把停用詞放在一個(gè)stopwords.txt文件中,將我們的數(shù)據(jù)與停用詞進(jìn)行比對(duì)即可(注:只要在百度中輸入stopwords.txt,就可以下載到該文件)。去停用詞代碼如下:

stopwords=pd.read_csv("stopwords.txt",index_col=False,quoting=3,sep="	",names=["stopword"], encoding="utf-8")#quoting=3全不引用
words_df=words_df[~words_df.segment.isin(stopwords.stopword)]

繼續(xù)使用words_df.head()語(yǔ)句來(lái)查看結(jié)果,如下圖所示,停用詞已經(jīng)被除去了。

接下來(lái)就要進(jìn)行詞頻統(tǒng)計(jì)了,代碼如下:

import numpy    #numpy計(jì)算包
words_stat=words_df.groupby(by=["segment"])["segment"].agg({"計(jì)數(shù)":numpy.size})
words_stat=words_stat.reset_index().sort_values(by=["計(jì)數(shù)"],ascending=False)

words_stat.head()進(jìn)行查看,結(jié)果如下:

由于我們前面只是爬取了第一頁(yè)的評(píng)論,所以數(shù)據(jù)有點(diǎn)少,在最后給出的完整代碼中,我爬取了10頁(yè)的評(píng)論,所以數(shù)據(jù)還是有一定參考價(jià)值的。

三、用詞云進(jìn)行顯示

代碼如下:

import matplotlib.pyplot as plt
%matplotlib inline
import matplotlib
matplotlib.rcParams["figure.figsize"] = (10.0, 5.0)
from wordcloud import WordCloud#詞云包

wordcloud=WordCloud(font_path="simhei.ttf",background_color="white",max_font_size=80) #指定字體類型、字體大小和字體顏色
word_frequence = {x[0]:x[1] for x in words_stat.head(1000).values}
word_frequence_list = []
for key in word_frequence:
    temp = (key,word_frequence[key])
    word_frequence_list.append(temp)

wordcloud=wordcloud.fit_words(word_frequence_list)
plt.imshow(wordcloud)

其中simhei.ttf使用來(lái)指定字體的,可以在百度上輸入simhei.ttf進(jìn)行下載后,放入程序的根目錄即可。顯示的圖像如下:

到此為止,整個(gè)項(xiàng)目的介紹就結(jié)束了。由于自己也還是個(gè)初學(xué)者,接觸python不久,代碼寫(xiě)的并不好。而且第一次寫(xiě)技術(shù)博客,表達(dá)的有些冗余,請(qǐng)大家多多包涵,有不對(duì)的地方,請(qǐng)大家批評(píng)指正。以后我也會(huì)將自己做的小項(xiàng)目以這種形式寫(xiě)在博客上和大家一起交流!最后貼上完整的代碼。

完整代碼
#coding:utf-8
__author__ = "hang"

import warnings
warnings.filterwarnings("ignore")
import jieba    #分詞包
import numpy    #numpy計(jì)算包
import codecs   #codecs提供的open方法來(lái)指定打開(kāi)的文件的語(yǔ)言編碼,它會(huì)在讀取的時(shí)候自動(dòng)轉(zhuǎn)換為內(nèi)部unicode 
import re
import pandas as pd  
import matplotlib.pyplot as plt
from urllib import request
from bs4 import BeautifulSoup as bs
%matplotlib inline
import matplotlib
matplotlib.rcParams["figure.figsize"] = (10.0, 5.0)
from wordcloud import WordCloud#詞云包

#分析網(wǎng)頁(yè)函數(shù)
def getNowPlayingMovie_list():   
    resp = request.urlopen("https://movie.douban.com/nowplaying/hangzhou/")        
    html_data = resp.read().decode("utf-8")    
    soup = bs(html_data, "html.parser")    
    nowplaying_movie = soup.find_all("div", id="nowplaying")        
    nowplaying_movie_list = nowplaying_movie[0].find_all("li", class_="list-item")    
    nowplaying_list = []    
    for item in nowplaying_movie_list:        
        nowplaying_dict = {}        
        nowplaying_dict["id"] = item["data-subject"]       
        for tag_img_item in item.find_all("img"):            
            nowplaying_dict["name"] = tag_img_item["alt"]            
            nowplaying_list.append(nowplaying_dict)    
    return nowplaying_list

#爬取評(píng)論函數(shù)
def getCommentsById(movieId, pageNum): 
    eachCommentList = []; 
    if pageNum>0: 
         start = (pageNum-1) * 20 
    else: 
        return False 
    requrl = "https://movie.douban.com/subject/" + movieId + "/comments" +"?" +"start=" + str(start) + "&limit=20" 
    print(requrl)
    resp = request.urlopen(requrl) 
    html_data = resp.read().decode("utf-8") 
    soup = bs(html_data, "html.parser") 
    comment_div_lits = soup.find_all("div", class_="comment") 
    for item in comment_div_lits: 
        if item.find_all("p")[0].string is not None:     
            eachCommentList.append(item.find_all("p")[0].string)
    return eachCommentList

def main():
    #循環(huán)獲取第一個(gè)電影的前10頁(yè)評(píng)論
    commentList = []
    NowPlayingMovie_list = getNowPlayingMovie_list()
    for i in range(10):    
        num = i + 1 
        commentList_temp = getCommentsById(NowPlayingMovie_list[0]["id"], num)
        commentList.append(commentList_temp)

    #將列表中的數(shù)據(jù)轉(zhuǎn)換為字符串
    comments = ""
    for k in range(len(commentList)):
        comments = comments + (str(commentList[k])).strip()

    #使用正則表達(dá)式去除標(biāo)點(diǎn)符號(hào)
    pattern = re.compile(r"[u4e00-u9fa5]+")
    filterdata = re.findall(pattern, comments)
    cleaned_comments = "".join(filterdata)

    #使用結(jié)巴分詞進(jìn)行中文分詞
    segment = jieba.lcut(cleaned_comments)
    words_df=pd.DataFrame({"segment":segment})

    #去掉停用詞
    stopwords=pd.read_csv("stopwords.txt",index_col=False,quoting=3,sep="	",names=["stopword"], encoding="utf-8")#quoting=3全不引用
    words_df=words_df[~words_df.segment.isin(stopwords.stopword)]

    #統(tǒng)計(jì)詞頻
    words_stat=words_df.groupby(by=["segment"])["segment"].agg({"計(jì)數(shù)":numpy.size})
    words_stat=words_stat.reset_index().sort_values(by=["計(jì)數(shù)"],ascending=False)

    #用詞云進(jìn)行顯示
    wordcloud=WordCloud(font_path="simhei.ttf",background_color="white",max_font_size=80)
    word_frequence = {x[0]:x[1] for x in words_stat.head(1000).values}

    word_frequence_list = []
    for key in word_frequence:
        temp = (key,word_frequence[key])
        word_frequence_list.append(temp)

    wordcloud=wordcloud.fit_words(word_frequence_list)
    plt.imshow(wordcloud)

#主函數(shù)
main()

結(jié)果顯示如下:

上圖基本反映了《戰(zhàn)狼2》這部電影的情況。PS:我本人并不喜歡這部電影,內(nèi)容太空洞、太假,為了愛(ài)國(guó)而愛(ài)國(guó),沒(méi)意思。哎,這兩年真是國(guó)產(chǎn)電影的低谷啊,沒(méi)有一部拿得出手的國(guó)產(chǎn)電影,看看人家印度拍的《摔跤吧,爸爸》那才是拍的有深度,同樣是表現(xiàn)愛(ài)國(guó),國(guó)產(chǎn)電影還是需要向別的國(guó)家好好學(xué)學(xué)。

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/40697.html

相關(guān)文章

  • 零基礎(chǔ)如何學(xué)爬蟲(chóng)技術(shù)

    摘要:楚江數(shù)據(jù)是專業(yè)的互聯(lián)網(wǎng)數(shù)據(jù)技術(shù)服務(wù),現(xiàn)整理出零基礎(chǔ)如何學(xué)爬蟲(chóng)技術(shù)以供學(xué)習(xí),。本文來(lái)源知乎作者路人甲鏈接楚江數(shù)據(jù)提供網(wǎng)站數(shù)據(jù)采集和爬蟲(chóng)軟件定制開(kāi)發(fā)服務(wù),服務(wù)范圍涵蓋社交網(wǎng)絡(luò)電子商務(wù)分類信息學(xué)術(shù)研究等。 楚江數(shù)據(jù)是專業(yè)的互聯(lián)網(wǎng)數(shù)據(jù)技術(shù)服務(wù),現(xiàn)整理出零基礎(chǔ)如何學(xué)爬蟲(chóng)技術(shù)以供學(xué)習(xí),http://www.chujiangdata.com。 第一:Python爬蟲(chóng)學(xué)習(xí)系列教程(來(lái)源于某博主:htt...

    KunMinX 評(píng)論0 收藏0
  • Vue 電影信息影評(píng)豆瓣,貓眼)

    摘要:界面如下圖電影詳情展示此功能是提供電影詳情的界面,包括演員上映時(shí)間導(dǎo)員劇情簡(jiǎn)介宣傳視頻宣傳視頻由于豆瓣官方的限制無(wú)法獲取,所以只有正在熱映板塊從貓眼獲取的電影信息才有。 Vue電影信息影評(píng)網(wǎng)站 此網(wǎng)站是我的畢業(yè)設(shè)計(jì),題目是基于HTML5的電影信息匯總弄網(wǎng)站,由于最近在看Vue.js,所以就想用Vue.js來(lái)構(gòu)建一個(gè)前端網(wǎng)站,這里code就不大篇幅的寫(xiě)了,主要講一下網(wǎng)站的功能和所用到的技...

    hzc 評(píng)論0 收藏0
  • Vue 電影信息影評(píng)豆瓣,貓眼)

    摘要:界面如下圖電影詳情展示此功能是提供電影詳情的界面,包括演員上映時(shí)間導(dǎo)員劇情簡(jiǎn)介宣傳視頻宣傳視頻由于豆瓣官方的限制無(wú)法獲取,所以只有正在熱映板塊從貓眼獲取的電影信息才有。 Vue電影信息影評(píng)網(wǎng)站 此網(wǎng)站是我的畢業(yè)設(shè)計(jì),題目是基于HTML5的電影信息匯總弄網(wǎng)站,由于最近在看Vue.js,所以就想用Vue.js來(lái)構(gòu)建一個(gè)前端網(wǎng)站,這里code就不大篇幅的寫(xiě)了,主要講一下網(wǎng)站的功能和所用到的技...

    Nekron 評(píng)論0 收藏0
  • 豆瓣電影,電視劇DM實(shí)戰(zhàn)

    摘要:用戶就我們這一個(gè)宿舍而其中電影的資源則是選擇來(lái)自有大量電影電視劇信息的豆瓣。爬蟲(chóng)應(yīng)對(duì)反爬蟲(chóng)機(jī)制目前來(lái)說(shuō)豆瓣的反爬蟲(chóng)機(jī)制算是可以接受。 前言 前段時(shí)間和室友鬧劇荒,于是萌生出一種做個(gè)私人化推薦系統(tǒng)想法。(用戶就我們這一個(gè)宿舍)而其中電影的資源則是選擇來(lái)自有大量電影電視劇信息的豆瓣。 目前,電影的信息已經(jīng)抓取完畢,共有11159條JSON格式的數(shù)據(jù),內(nèi)容包括: 題目 時(shí)長(zhǎng) 類型 導(dǎo)演 主...

    phpmatt 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

閱讀需要支付1元查看
<