python 豆瓣评论分析方法_基于Python的豆瓣影评分析——数据预处理
一、數(shù)據(jù)抓取
利用python軟件,抓取豆瓣網(wǎng)上關(guān)于《向往的生活》的影視短評(píng),進(jìn)行網(wǎng)頁(yè)爬蟲,爬蟲代碼如下:import requests
import re
cookies=[cookie1,cookie2,cookie3]
url1='https://movie.douban.com/subject/26873657/comments?start='? #《向往的生活 第1季》豆瓣短評(píng)網(wǎng)址
url2='https://movie.douban.com/subject/27602222/comments?start='
#《向往的生活 第2季》豆瓣短評(píng)網(wǎng)址
url3='https://movie.douban.com/subject/30441625/comments?start='
#《向往的生活 第3季》豆瓣短評(píng)網(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)頁(yè)
url = urls[i] +str(n) + '&limit=20&sort=new_score&status=P'
# print(url)
# print(n)
#打開(kāi)目錄,并獲取內(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ù)如下:
部分訓(xùn)練集
二、數(shù)據(jù)預(yù)處理
由于原始的影評(píng)文本中可能存在與情感分析任務(wù)無(wú)關(guān)的內(nèi)容以及軟件無(wú)法識(shí)別的特殊字符表情,因此要對(duì)文本進(jìn)行預(yù)處理操作。數(shù)據(jù)預(yù)處理分為中文分詞和去除停用詞兩個(gè)步驟。
(1)中文分詞
中文分詞就是將一整句中文文本,經(jīng)過(guò)切分形成單個(gè)單獨(dú)詞語(yǔ)的過(guò)程,本文采取的中文分詞工具為jieba分詞,詞與詞之間用空格隔開(kāi)。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)去除停用詞
為有效提高運(yùn)行時(shí)間,節(jié)省儲(chǔ)存空間,我們將采取去除停用詞的方法,本文采取的停用詞都是感情色彩較弱或不具有感情色彩的詞匯。數(shù)據(jù)在進(jìn)行上述兩個(gè)步驟之后,形成的txt文件部分內(nèi)容如下:
數(shù)據(jù)預(yù)處理部分結(jié)果
三、描述性統(tǒng)計(jì)
將預(yù)處理得到的數(shù)據(jù)導(dǎo)入到Python中,手動(dòng)去除幾個(gè)出現(xiàn)頻率高但不包含情感的詞匯后,把文本中出現(xiàn)頻率較高的關(guān)鍵詞,在視覺(jué)效果上給予突出,形成關(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()
文本評(píng)論中“喜歡”、“綜藝”和“生活”屬于用戶共同的話題,文字越大表明用戶的情感越強(qiáng)烈,綜合以上信息,可得大多數(shù)人認(rèn)為《向往的生活》這一綜藝是好看的,大多數(shù)用戶是喜歡的,節(jié)目中的大華也得到了一致好評(píng)。但詞云圖中也出現(xiàn)了“抄襲”、“尷尬”這些話題,說(shuō)明有一部分評(píng)論者認(rèn)為節(jié)目存在抄襲現(xiàn)象,相處模式比較尷尬,這就為創(chuàng)作者提供了很好的改進(jìn)來(lái)源。創(chuàng)作者在節(jié)目原創(chuàng)部分有待提高,劇本和嘉賓相處模式可以更加流暢。
影視評(píng)論詞云圖
總結(jié)
以上是生活随笔為你收集整理的python 豆瓣评论分析方法_基于Python的豆瓣影评分析——数据预处理的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 《善用佳软:高效能人士的软件应用之道》一
- 下一篇: 豆瓣电影爬虫Ⅱ 豆瓣电影数据的整理