Python超簡單分析評論提取關(guān)鍵詞制作精美詞云流程
?一、抓取全部評論
吾的這篇文章,有 1022 次評論,一條條看,吾看不過來,于是想到 Python 詞云,提取關(guān)鍵詞,倒也是一樁趣事。

評論情況: {'android': 545 次, 'ios': 110 次, 'pc': 44 次, 'uniapp': 1 次}
一個小細(xì)節(jié):給我評論的設(shè)備中,安卓蘋果比是 5:1。
Building prefix dict from the default dictionary ... Loading model cost 0.361 seconds. Prefix dict has been built successfully.
1、找到評論接口
- 打開 chrome 瀏覽器,開發(fā)者模式
- 點(diǎn)擊評論列表(圖標(biāo) 1)
- 點(diǎn)擊接口鏈接(圖標(biāo) 2)
- 查看 response 返回值(評論結(jié)果的 json 格式)

2、Python 獲取評論
def get_comments(articleId):
# 確定評論的頁數(shù)
main_res = get_commentId(articleId,1)
pageCount = json.loads(main_res)['data']['pageCount']
comment_list,comment_list2 = [],[]
source_analy = {}
for p in range(1,pageCount+1):
res = get_commentId(articleId, p)
try:
commentIds = json.loads(res)['data']['list']
for i in commentIds:
commentId = i['info']['commentId']
userName = i['info']['userName']
nickName = i['info']['nickName'] ## 獲取用戶名
source_dvs = i['info']['commentFromTypeResult']['key'] # 操作設(shè)備
content = i['info']['content']
comment_list.append([commentId, userName, nickName, source_dvs, content])
comment_list2.append("%s 丨 %s"%(userName, nickName))
if source_dvs not in source_analy.keys():
source_analy[source_dvs] = 1
else:
source_analy[source_dvs] = source_analy[source_dvs] + 1
# print(source_analy)
except:
print('本頁失??!')
print('評論數(shù):' + str(len(comment_list)))
return source_analy, comment_list, comment_list2
二、文本分詞、詞云制作
1、文本分析
西紅柿采用的是 結(jié)巴 分詞, 和 wordcloud。
# -*- coding:utf8 -*- import jieba import wordcloud
代碼實(shí)現(xiàn):
seg_list = jieba.cut(comments, cut_all=False) # 精確模式
word = ' '.join(seg_list)
2、生成詞云
背景圖 西紅柿采用的是 心形圖片
pic = mpimg.imread('/Users/pray/Downloads/aixin.jpeg')
完整代碼::
def word_cloud(articleId):
source_analy, comment_list, comment_list2 = get_comments(articleId)
print("評論情況:", source_analy)
comments = ''
for one in comment_list:
comment = one[4]
if 'face' not in comment:
comments = comments + comment
seg_list = jieba.cut(comments, cut_all=False) # 精確模式
word = ' '.join(seg_list)
pic = mpimg.imread('/Users/pray/Downloads/aixin.jpeg')
wc = wordcloud.WordCloud(mask=pic, font_path='/Library/Fonts/Songti.ttc', width=1000, height=500,
background_color='white').generate(word)
3、初步效果-模糊不清

西紅柿發(fā)現(xiàn)文字模糊、圖像曲線邊緣不清晰的問題。
于是,指定分辨率,高清整起來。
# 保存
plt.savefig('xin300.png', dpi=300) #指定分辨率保存
4、最終效果-高清無馬

到此這篇關(guān)于Python超簡單分析評論提取關(guān)鍵詞制作精美詞云流程的文章就介紹到這了,更多相關(guān)Python 制作詞云內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python如何實(shí)現(xiàn)數(shù)據(jù)的線性擬合
這篇文章主要為大家詳細(xì)介紹了python如何實(shí)現(xiàn)數(shù)據(jù)的線性擬合,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-07-07
python爬蟲selenium和phantomJs使用方法解析
這篇文章主要介紹了python爬蟲selenium和phantomJs使用方法解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-08-08

