需要安装jieba、matplotlib、numpy、PIL、wordcloud等库
import requests
import json
import os
import time
import random
import jieba
import numpy as np
from PIL import Image
from wordcloud import WordCloud
import matplotlib.pyplot as plt
url1 = 'https://service.danmu.youku.com/list?jsoncallback=jQuery111205985606965939538_1566299621035&mat=6&mcount=1&ct=1001&iid=1061138103&aid=322943&cid=97&lid=0&ouid=0&_=1566299621063'
url2 = "https://service.danmu.youku.com/list?jsoncallback=jQuery111207570414009967952_1566258556628&mat=1&mcount=1&ct=1001&iid=1061115893&aid=322943&cid=97&lid=0&ouid=0&_=1566258556647"
url3 = 'https://service.danmu.youku.com/list?jsoncallback=jQuery111208298124826441615_1566297247536&mat=0&mcount=1&ct=1001&iid=1061156738&aid=322943&cid=97&lid=0&ouid=0&_=1566297247554'
DANMU_FILE_PATH = '/home/adog/PycharmProjects/uk/DANMU_FILE_PATH.txt'
WC_FONT_PATH = '/usr/share/fonts/truetype/windows-fonts/1.ttf'
def spider_danmu(url_i):
"""
爬取某个指定页的弹幕
"""
kv = {'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36',
'Referer':'https://v.youku.com/v_show/id_XNDI0NDQ2MzU3Mg==.html?spm=a2h0k.11417342.soresults.dselectbutton&s=efbfbd78efbfbd5cefbf',
'Sec-Fetch-Mode':'no-cors'}
try:
r = requests.get(url_i,headers=kv)
except:
print('爬取失败!')
#print(r.text)
# 找到json数据的左括号位置并+1
json_start_index = r.text.index('(') + 1
# 截取json字符串
r_json_str = r.text[json_start_index:-2]
print(r_json_str)
# json字符串转换为json对象
r_json_obj = json.loads(r_json_str)
print(r_json_obj)
r_json_result = r_json_obj['result']
for r_json_damu in r_json_result:
# 打印并输出评论内容
print(r_json_damu['content'])
# 以追加模式写入每条评论
with open(DANMU_FILE_PATH,'a+') as fil:
fil.write(r_json_damu['content'] + '\n')
def cut_word():
with open(DANMU_FILE_PATH) as file:
comment_txt = file.read()
word_list = jieba.cut(comment_txt,cut_all=False)
wl = " ".join(word_list)
print(wl)
return wl
def create_word_cloud():
"""生成词云"""
# 设置词云形状图片
#wc_mask = np.array(Image.open(WC_MASK_IMG))
# 设置词云的一些配置,如:字体、背景色、词云形状、大小
wc = WordCloud(background_color='white',max_words=2000,width=800,height=600,
max_font_size=50,random_state=42,font_path=WC_FONT_PATH)
# 生成词云
wc.generate(cut_word())
# 在只设置mask的情况下,你将会得到一个拥有图片形状的词云
plt.rcParams['savefig.dpi'] = 300 # 图片像素
plt.rcParams['figure.dpi'] = 300 # 分辨率
plt.imshow(wc, interpolation='bilinear')
plt.axis("off")
plt.show()
if __name__ == '__main__':
# spider_danmu()
create_word_cloud()
# spider_danmu(url3)
效果:
参考:链接