Python超简单分析评论提取关键词制作精美词云流程

2022-07-16,,,,

 一、抓取全部评论

吾的这篇文章,有 1022 次评论,一条条看,吾看不过来,于是想到 python 词云,提取关键词,倒也是一桩趣事。

评论情况: {'android': 545 次, 'ios': 110 次, 'pc': 44 次, 'uniapp': 1 次}

一个小细节:给我评论的设备中,安卓苹果比是 5:1。

building prefix dict from the default dictionary ... loading model cost 0.361 seconds. prefix dict has been built successfully.

1、找到评论接口

  • 打开 chrome 浏览器,开发者模式
  • 点击评论列表(图标 1)
  • 点击接口链接(图标 2)
  • 查看 response 返回值(评论结果的 json 格式)

2、python 获取评论

def get_comments(articleid):
    # 确定评论的页数
    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']   # 操作设备
                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('评论数:' + str(len(comment_list)))
    return source_analy, comment_list, comment_list2

二、文本分词、词云制作

1、文本分析

西红柿采用的是 结巴 分词, 和 wordcloud。

# -*- coding:utf8 -*-
import jieba
import wordcloud

代码实现:

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、初步效果-模糊不清

西红柿发现文字模糊、图像曲线边缘不清晰的问题。

于是,指定分辨率,高清整起来。

# 保存
plt.savefig('xin300.png', dpi=300) #指定分辨率保存

4、最终效果-高清无马

到此这篇关于python超简单分析评论提取关键词制作精美词云流程的文章就介绍到这了,更多相关python 制作词云内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

《Python超简单分析评论提取关键词制作精美词云流程.doc》

下载本文的Word格式文档,以方便收藏与打印。