1.获取到json数据
import requests
#https://club.jd.com/comment/productPageComments.action?callback=fetchJSON_comment98&productId=100016944073&score=0&sortType=5&page=2&pageSize=10&isShadowSku=0&rid=0&fold=1
url = "https://club.jd.com/comment/productPageComments.action?callback=fetchJSON_comment98&productId=100016944073&score=0&sortType=5&pageSize=10&isShadowSku=0&fold=1"
headers ={
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36'
}
param ={
'page':0
}
response = requests.get(url=url,headers=headers,params=param)
content = response.content.decode('GBK')
print(content)
with open('one.json','w',encoding='utf-8') as x:
x.write(content)
print(response.encoding)
2.提取数据
import json
import jsonpath
obj = json.load(open('one.json','r',encoding='utf-8'))
res = jsonpath.jsonpath(obj,'$..content')
# print(obj)
print(res)
2.1结果
3.词云
import jieba
from wordcloud import WordCloud
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
with open('words.txt','r',encoding="UTF-8") as f:
file = f.read()
# print(file)
data_cut = jieba.cut(file, cut_all=False)
print(data_cut)
stop_words = [",", "。", ";", "、", "?", "]","["]
data_result = [i for i in data_cut if i not in stop_words] # 获取需要的词
# print(data_result)
text = " ".join(data_result).replace("\", "")
# print(text)
wc = WordCloud(font_path = "YanShiYouRanXiaoKai-2.ttf", background_color = "black", max_words = 5000)
wc.generate(text)
wc.to_file("xdn.jpg")
plt.imshow(wc)
plt.axis("off")
plt.show()
3.1结果