python爬取弹幕读取csv文件制作弹幕热点词云图-万恶之源马老师-后裔弃兵

python3.8
jupyter notebook

1.后裔弃兵

《后翼弃兵》豆瓣短评数据集

import pandas
import csv
import jieba
import numpy
from PIL import Image
from wordcloud import WordCloud
def getDataFromCsv():
    # 设置星级等级,根据等级来定位提取弹幕
    stars = ("1","2","3","4","5")
    # 设置空列表,装从表格里面读出来的所有数据
    comments = []
    # 打开表格,"r"读取模式  读取数据
    with open("1.csv","r",encoding="utf-8") as file:
        # 表格操作读数据
        reader = csv.reader(file)
        # 遍历表格里得到所有数据     [用户名,星级,评论]
        for i in reader:
            # 如果没有星级
            if i[8] not in stars:
                # 数据无效,忽略不处理
                pass
            else:
                # 数据有效,装入数组
                comments.append(i)
        # print(comments)
        file.close()
    # 将装有数据的列表返回出来
    return comments
getDataFromCsv()
[['2020-11-12 14:11:28.928',
  '后翼弃兵',
  '32579283',
  'https://movie.douban.com/subject/32579283/comments?sort=new_score&status=P',
  '#未注销#',
  'https://www.douban.com/people/thedarknine/',
  '\n            \n                网飞出品,多 半成品,这可是久违的“丝滑”。神童,但不神化。“山川异域,风月同天”,何尝不是一个江湖故事。\n        ',
  '\n                    2020-10-23\n                ',
  '4',
  '1368'],


 ['2020-11-12 14:11:28.928',
  '后翼弃兵',
  '32579283',
  'https://movie.douban.com/subject/32579283/comments?sort=new_score&status=P',
  '转发点赞五星',
  'https://www.douban.com/people/26414546/',
  '\n            \n                这个剧情感觉有点平淡,但是我为什么看完了。男人都想帮你,但其实是想睡你,但其实还是想帮你。没啥,我也想生活在俄罗斯。\n        ',
  '\n                    2020-10-24\n                ',
  '5',
  '1407'],
 
 ['2020-11-12 14:11:28.928',
  '后翼弃兵',
  '32579283',
  'https://movie.douban.com/subject/32579283/comments?sort=new_score&status=P',
  'fushia',
  'https://www.douban.com/people/gotothefield/',
  '\n            \n                国际象棋版麦瑟尔夫人+美国夫人+梅尔罗斯。最后一集好俗气啊但我仍然不能免俗地在每一个泪点留下眼泪。\n        ',
  '\n                    2020-10-27\n                ',
  '4',
  '634'], ['2020-11-12 14:11:28.928',
  '后翼弃兵',
  '32579283',
  'https://movie.douban.com/subject/32579283/comments?sort=new_score&status=P',
  '你说什么都对',
  'https://www.douban.com/people/limiaolm/',
  '\n            \n                关键时刻还是姐妹比男人靠谱\n        ',
  '\n                    2020-11-05\n                ',
  '4',
  '0']]
# 定义函数,将解析的评论做成词云
def getWordCloud():
    # 调用函数:得到表格中所有的数据
    data = getDataFromCsv()
    # 定义空的字符串,把所有的评论装进来
    str = ""
    # 遍历所有的数据
    for i in data:
        # [用户名, 星级, 评论]
        str+=i[6]
    print(str)
    # 通过jieba分词器将评论里面的词语用空格分离出来
    cutWord = " ".join(jieba.cut(str))
    # print(cutWord)
    # 读取图片模型
#     bgImg = numpy.array(Image.open("a.jpg"))
    # 准备词云参数
    cloud = WordCloud(
        # 文字的路径:本地的系统文件路径
        font_path="C:\Windows\Fonts\STZHONGS.TTF",
        # 生成词云的图片背景
        background_color="white",max_words=1300,margin=3,width=1800,height=800,random_state=42
        # 参考图片(参数,没有引号)
#         mask=bgImg
    ).generate(cutWord)
    # 将做成的结果生成图片
    cloud.to_file("ciyun.png")
getWordCloud()

在这里插入图片描述

2.万恶之源弹幕

数据csv

import pandas
import csv
import jieba
import numpy
from PIL import Image
from wordcloud import WordCloud
def getDataFromCsv():
    # 设置星级等级
    comments = []
    # 打开表格,"r"读取模式  读取数据
    with open(r"E:\01_hjz\datas\01_ml\paulmadanmaku.csv","r",encoding="utf-8") as file:
        # 表格操作读数据
        reader = csv.reader(file)
        # 遍历表格里得到所有数据   
        next(reader)#从第二行开始读取
        for i in reader:
            # 如果没有星级
            comments.append(i)
        print(comments)
        file.close()
    # 将装有数据的列表返回出来
    return comments
getDataFromCsv()
[['0 days 00:01:16.562000000',
  '2020-01-05',
  '2020-01',
  '2020-01-05 15:58:45',
  '你要感谢人家小伙子点到为止,不然你眼睛保不住。',
  '7ecdfd8'],
  ['0 days 00:00:59.156000000',
  '2020-05-18',
  '2020-05',
  '2020-05-18 04:49:33',
  '已经被打死了',
  'fb2f37fe'],
 ...]
# 定义函数,将解析的评论做成词云
def getWordCloud():
    # 调用函数:得到表格中所有的数据
    data = getDataFromCsv()
    # 定义空的字符串,把所有的评论装进来
    str = ""
    # 遍历所有的数据
    for i in data:
        # [用户名, 星级, 评论]
        str+=i[4]
    print(str)
    # 通过jieba分词器将评论里面的词语用空格分离出来
    cutWord = " ".join(jieba.cut(str))
    # print(cutWord)
    # 读取图片模型
#     bgImg = numpy.array(Image.open("a.jpg"))
    # 准备词云参数
    cloud = WordCloud(
        # 文字的路径:本地的系统文件路径
        font_path="C:\Windows\Fonts\STZHONGS.TTF",
        # 生成词云的图片背景
        background_color="white",max_words=1300,margin=3,width=1800,height=800,random_state=42
        # 参考图片(参数,没有引号)
#         mask=bgImg
    ).generate(cutWord)
    # 将做成的结果生成图片
    cloud.to_file("ciyun.png")
getWordCloud()
Dumping model to file cache C:\Users\ADMINI~1\AppData\Local\Temp\jieba.cache
Loading model cost 0.605 seconds.
Prefix dict has been built successfully.

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值