python豆瓣爬取评论并生成词云

需要准备的库:

request----------------爬虫包
re-----------------------正则包
wordcloud------------词云
matplotlib.pyplot----处理生成图片
jieba-------------------处理词云中文乱码

话不多说直接上成品图

在这里插入图片描述

源码在此(原理简单,在此不做过多赘述):

import requests
import re
from wordcloud import WordCloud
import matplotlib.pyplot as plt
import jieba


Referer = "https://movie.douban.com/subject/35096844/comments?status=P"
headers = {

    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36 Edg/89.0.774.54"
}

respone = requests.get(headers=headers, url=Referer).text

reg = r"""<span class="short">(.*?)</span>"""
reg = re.findall(reg, respone)

with open('ttt.txt', 'w') as f:
    for i in reg:
        f.write(i)
        f.write('\n')


def read_fun():
    with open("ttt.txt", "r") as f:
        txt = f.read()

    re_move = [",", "。", "\n", "\xa0"]
    for i in re_move:
        txt = txt.replace(i, " ")
    word = jieba.lcut(txt)
    with open("txt_save.txt", "w") as file:
        for i in word:
            file.write(str(i) + " ")
    print('文本处理完成')


def imag_fun():
    with open("txt_save.txt", "r") as fp:
        txt = fp.read()

    WCloud = WordCloud(
        width=800,
        height=800,
        font_path="simhei.ttf").generate(txt)
    WCloud.to_file("test.png")
    plt.imshow(WCloud)
    plt.axis('off')
    plt.show()


if __name__ == '__main__':
    read_fun()
    imag_fun()

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值