用Python词云看电影--生而为人,对不起

选取的是《被嫌弃的松子的一生》
在这里插入图片描述
词云效果:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
完整代码:

# 分析豆瓣被嫌弃的松子的一生的影评,生成词云
# https://movie.douban.com/subject/1787291/comments?start=20&limit=20&status=P&sort=new_score
# url = 'https://movie.douban.com/subject/%s/comments?start=%s&limit=20&sort=new_score&status=P '\
# % (movie_id, (i - 1) * 20)

import requests
from stylecloud import gen_stylecloud
import jieba
import re
from bs4 import BeautifulSoup
from wordcloud import STOPWORDS

headers = {
     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0'
}




def jieba_cloud(file_name, icon):
    with open(file_name, 'r', encoding='utf8') as f:
        word_list = jieba.cut(f.read())

        result = " ".join(word_list)    # 分词用  隔开

        # 设置停用词
        stopwords_file = open('stopwords.txt', 'r', encoding='utf-8')
        stopwords = [words.strip() for words in stopwords_file.readlines()]


        # 制作中文词云
        icon_name = " "
        if icon == "1":
            icon_name = "fas fa-grin-hearts"
        elif icon == "2":
            icon_name = "fas fa-space-shuttle"
        elif icon == "3":
            icon_name = "fas fa-heartbeat"
        elif icon == "4":
            icon_name = "fas fa-bug"
        elif icon == "5":
            icon_name = "fas fa-thumbs-up"
        elif icon == "6":
            icon_name = "fab fa-qq"
        pic = str(icon) + '.png'
        if icon_name is not None and len(icon_name) > 0:
            gen_stylecloud(text=result,
                           size=1024,  # stylecloud 的大小(长度和宽度)
                           icon_name=icon_name,
                           font_path='simsun.ttc',
                           max_font_size=200,  # stylecloud 中的最大字号
                           max_words=2000,  # stylecloud 可包含的最大单词数
                           #stopwords=TRUE,  # 布尔值,用于筛除常见禁用词
                           custom_stopwords=stopwords,   #定制停用词列表
                           output_name=pic)
        else:
            gen_stylecloud(text=result, font_path='simsun.ttc', output_name=pic)
        return pic


def spider_comment(movie_id, page):
    comment_list = []
    with open("douban.txt", "a+", encoding='utf-8') as f:
        for i in range(1,page+1):

            url = 'https://movie.douban.com/subject/%s/comments?start=%s&limit=20&sort=new_score&status=P' \
                  % (movie_id, (i - 1) * 20)

            req = requests.get(url, headers=headers)
            req.encoding = 'utf-8'
            comments = re.findall('<span class="short">(.*)</span>', req.text)


            f.writelines('\n'.join(comments))
    print(comments)

# 主函数
if __name__ == '__main__':
    movie_id = '1787291'
    page = 10
    spider_comment(movie_id, page)

    jieba_cloud("douban.txt", "1")
    jieba_cloud("douban.txt", "2")
    jieba_cloud("douban.txt", "3")
    jieba_cloud("douban.txt", "4")
    jieba_cloud("douban.txt", "5")

    jieba_cloud("douban.txt", "6")

爬取过程参考:

Python爬取你好李焕英豆瓣短评并利用stylecloud制作更酷炫的词云图

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BugMiaowu2021

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值