python绘制词云 # 导入依赖模块 import json import requests import jieba import pandas as pd import wordcloud import numpy as np import PIL . Image as image import matplotlib . pyplot as plt from pandas . io . json import json_norma
python绘制词云
# 导入依赖模块import json
import requests
import jieba
import pandas as pd
import wordcloud
import numpy as np
import PIL.Image as image
import matplotlib.pyplot as plt
from pandas.io.json import json_normalize
# 请求头
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.106 Safari/537.36'}
# 评论地址
url="http://comment.api.163.com/api/v1/products/a2869674571f77b5a0867c3d71db5856/threads/FASTLQ7I00038FO9/comments/newList?ibc=newspc&limit=30&showLevelThreshold=72&headLimit=1&tailLimit=2&offset={}"
# 循环爬取
df = pd.DataFrame(None)
i = 0
while True:
# format拼接url
ret = requests.get(url.format(str(i*30)), headers=headers)
text = ret.text
result = json.loads(text)
t = result['comments'].values()
s = json_normalize(t)
i += 1
if len(s) == 0 or i==3:
# if len(s) == 0 :
print("爬取结束")
break
else:
df = df.append(s)
print("第{}页爬取完毕".format(i))
# 评论去重
df=df.drop_duplicates('commentId').reset_index(drop=True)
df.head()
# 评论分词
word = [
y
for x in df.content.tolist()
for y in list(jieba.cut(x))
]
# 读取词云模板
mask = np.array(image.open("D:/gua.jpg"))
# 设置停用词
stopword=[
'一个','这个','不是','就是','没有','什么','这么','这样',
'这种','怎么','斜眼','微笑','喷水','大笑','跟帖','br'
]
# 设置词云图层属性
wd=wordcloud.WordCloud(font_path='C:/windows/fonts/simhei.ttf', stopwords=stopword, mask=mask, collocations=False)
# 创建词云对象
wd=wd.generate(",".join(word))
# 将词云模板的颜色设置为输出词云的颜色
image_colors = wordcloud.ImageColorGenerator(mask) # 从背景图建立颜色方案
wd.recolor(color_func=image_colors)
# 绘制词云
plt.imshow(wd)
# 隐藏坐标轴
plt.axis('off')
# 画图
plt.show()
数据样式:
参考:https://mp.weixin.qq.com/s/nzY0HJXxCulRziBBdUn4Vg