一牍鞠、數(shù)據(jù)抓取
? ? ? ?利用python軟件,抓取豆瓣網(wǎng)上關(guān)于《向往的生活》的影視短評评姨,進(jìn)行網(wǎng)頁爬蟲难述,爬蟲代碼如下:
import requests
import re
cookies=[cookie1,cookie2,cookie3]
url1='https://movie.douban.com/subject/26873657/comments?start='? #《向往的生活 第1季》豆瓣短評網(wǎng)址
url2='https://movie.douban.com/subject/27602222/comments?start='?? #《向往的生活 第2季》豆瓣短評網(wǎng)址
url3='https://movie.douban.com/subject/30441625/comments?start='?? #《向往的生活 第3季》豆瓣短評網(wǎng)址
urls=[url1,url2,url3]
comments=[]
for i in range(0,3):
??? print(urls[i])
??? for n in range(0,500, 20):
??????? #獲取目標(biāo)網(wǎng)頁
??????? url = urls[i] +str(n) + '&limit=20&sort=new_score&status=P'
??????? # print(url)
??????? # print(n)
??????? #打開目錄,并獲取內(nèi)容
??????? html =requests.get(url, cookies=cookies[i])
??????? # print(html)
??????? #print(html.text)
??????? #篩選數(shù)據(jù)
??????? data =re.findall('(.*?)', html.text)
??????? for j inrange(len(data)):
??????????? comments.append(data[j])
print(comments)
with open('E:/熱心市民與好好先生/me/畢設(shè)/數(shù)據(jù)/data.txt','w',encoding='utf-8')as f:
??? #f.writelines(comments)
??? for comment incomments:
???????f.write(comment+"\n")
? ? ? ?由于豆瓣網(wǎng)設(shè)有爬蟲限制頻率吐句,所以抓取《向往的生活》三季的1468條數(shù)據(jù)作為訓(xùn)練數(shù)據(jù)集胁后,并保存為txt文件。訓(xùn)練集的部分?jǐn)?shù)據(jù)如下:
二嗦枢、數(shù)據(jù)預(yù)處理
? ? ? ?由于原始的影評文本中可能存在與情感分析任務(wù)無關(guān)的內(nèi)容以及軟件無法識別的特殊字符表情攀芯,因此要對文本進(jìn)行預(yù)處理操作。數(shù)據(jù)預(yù)處理分為中文分詞和去除停用詞兩個步驟文虏。
(1)中文分詞
? ? ? ?中文分詞就是將一整句中文文本侣诺,經(jīng)過切分形成單個單獨詞語的過程,本文采取的中文分詞工具為jieba分詞氧秘,詞與詞之間用空格隔開年鸳。
import jieba? #Python載入包
def preHandler(sentence):
??? seg_list=jieba.cut(sentence)
???f_stop=open('stopwords.txt','r')
??? try:
???????f_stop_text=f_stop.read()
??? finally:
??????? f_stop.close()
???f_stop_seg_list=f_stop_text.split('\n')
??? words=[]
??? for word inseg_list:
??????? ifnot(word.strip() in f_stop_seg_list):
???????????words.append(word)
??? return words
fw=open('E:/熱心市民與好好先生/me/畢設(shè)/數(shù)據(jù)/predata.txt','w', encoding='utf-8')
with open('E:/熱心市民與好好先生/me/畢設(shè)/數(shù)據(jù)/data.txt','r', encoding='utf-8') as f:
??? for line inf.readlines():
??????? line=line.strip()
???????word=preHandler(line)
??????? fw.write("".join(i for i in word)+"\n")
fw.close()
(2)去除停用詞
? ? ? ?為有效提高運行時間,節(jié)省儲存空間丸相,我們將采取去除停用詞的方法搔确,本文采取的停用詞都是感情色彩較弱或不具有感情色彩的詞匯。數(shù)據(jù)在進(jìn)行上述兩個步驟之后灭忠,形成的txt文件部分內(nèi)容如下:
三膳算、描述性統(tǒng)計
? ? ? ?將預(yù)處理得到的數(shù)據(jù)導(dǎo)入到Python中,手動去除幾個出現(xiàn)頻率高但不包含情感的詞匯后弛作,把文本中出現(xiàn)頻率較高的關(guān)鍵詞涕蜂,在視覺效果上給予突出,形成關(guān)鍵詞云層映琳。繪制詞云圖Python代碼如下:
def random_color_func(word=None, font_size=None,position=None, orientation=None, font_path= None, random_state=None):
??? h = randint(0, 0)
??? s = int(0 * 0 /255.0)
??? l = int(0 *float(randint(60, 120)) / 255.0)
??? return"hsl({}, {}%, {}%)".format(h, s, l)
with open(r'predata.txt','r',encoding="utf-8") asfile1:
? content ="".join(file1.readlines())
images = Image.open("ci.png") #讀取背景圖片宇葱,
maskImages = np.array(images)
wc = WordCloud(font_path="msyh.ttc",
??????????????background_color="white",
??????????????mask=maskImages,
??????????????max_words=1000,
??????????????max_font_size=300,
??????????????#color_func=random_color_func
??????????????).generate(content)
plt.imshow(wc)
plt.axis("off")
wc.to_file('laomao.png')
plt.show()
? ? ? ?文本評論中“喜歡”瘦真、“綜藝”和“生活”屬于用戶共同的話題,文字越大表明用戶的情感越強(qiáng)烈黍瞧,綜合以上信息诸尽,可得大多數(shù)人認(rèn)為《向往的生活》這一綜藝是好看的,大多數(shù)用戶是喜歡的印颤,節(jié)目中的大華也得到了一致好評您机。但詞云圖中也出現(xiàn)了“抄襲”、“尷尬”這些話題年局,說明有一部分評論者認(rèn)為節(jié)目存在抄襲現(xiàn)象际看,相處模式比較尷尬,這就為創(chuàng)作者提供了很好的改進(jìn)來源矢否。創(chuàng)作者在節(jié)目原創(chuàng)部分有待提高仲闽,劇本和嘉賓相處模式可以更加流暢。