python制作京东评论词云图
发布时间
阅读量:
阅读量
python制作京东评论词云图
在此基础上完成词频统计
首先我们把之前爬取的10页评论保存到文本文件
import jieba
import wordcloud
for page in range(0,10+1):
header = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36"}
url = 'https://club.jd.com/comment/productPageComments.action?callback=fetchJSON_comment98&productId=5225346&score=0&sortType=5&page={}&pageSize=10&isShadowSku=0&rid=0&fold=1'.format(page)
response = requests.get(url, headers=header)
data = response.text
jd = json.loads(data.lstrip('fetchJSON_comment98vv12345(').rstrip(');'))
data_list = jd['comments']
for data in data_list:
content = data['content']
# print(content)
with open('ci.txt','a+') as f:
f.write(content + '\n')
随后
with open('ci.txt') as f,open("stopword.txt") as f2:
stopwords = f2.read()
content = f.read()
wordlist = jieba.cut(content)
words = []
for word in wordlist:
if len(word)>1 and word not in stopwords:
words.append(word)
wc = wordcloud.WordCloud(width=1000, font_path='simfang.ttf',height=800)#设定词云画的大小字体,一定要设定字体,否则中文显示不出来
wc.generate(' '.join(words))
wc.to_file(r'C:\Users\Administrator\Desktop\python\项目\爬虫\京东评论\siyun.png')

参考这张图表可知,并主要涉及的因素包括运行速度、系统性能、屏幕质量以及散热效率等。该款电脑受到了很多用户的关注,并相对容易就能生成一些词云分析结果。
当然,我们还可以将背景图换掉。

from PIL import Image
import numpy as np
img = Image.open(r'C:\Users\Administrator\Desktop\图片\京东\129.jpg')
resized = np.array(img)
wc_1 = wordcloud.WordCloud(
background_color='white',
width=1000,
height=800,
mask=resized,
font_path='simfang.ttf'
)
wc_1.generate_from_text(' '.join(words))
wc_1.to_file(r'C:\Users\Administrator\Desktop\python\项目\爬虫\京东评论\siyun1.png')

好了,简单的词云图就产生啦,大家可以试一试,非常简单。
全部评论 (0)
还没有任何评论哟~
