项目地址:
https://github.com/NanmiCoder/MediaCrawler
cd MediaCrawler
python -m venv venv
# macos & linux 激活虚拟环境
source venv/bin/activate
# windows 激活虚拟环境
venv\Scripts\activate
pip install -r requirements.txt
安装 playwright浏览器驱动
playwright install
读取detail comments 2024-11-12,json里面的list数组的每个json里面的content的值作为词云数据
import json
import jieba
from wordcloud import WordCloud
import matplotlib.pyplot as plt
# 读取JSON文件
with open('detail_comments_2024-11-12.json', 'r', encoding='utf-8') as file:
data = json.load(file)
# 提取所有评论内容
comments = [item['content'] for item in data]
# 将所有评论合并为一个字符串
text = ' '.join(comments)
# 使用jieba进行中文分词
words = jieba.cut(text)
# 定义要去除的特定词汇
stopwords = ['我', '你', '了','的','是','上','下','一秒 :','一秒',':']
# stopwords = []
# 过滤掉特定词汇
filtered_words = [word for word in words if word not in stopwords]
# 将过滤后的分词结果转换为字符串
words_str = ' '.join(filtered_words)
# 创建词云对象
wordcloud = WordCloud(font_path='simhei.ttf', # 设置字体路径以支持中文显示
width=1600,
height=800,
background_color='white').generate(words_str)
# 显示词云
plt.figure(figsize=(20, 10))
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis('off') # 不显示坐标轴
plt.show()
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。