Python采集《雪中悍刀行》视频弹幕,并且做词云图可视化分析
前言
最近已经播完第一季的电视剧《雪中悍刀行》,从播放量就可以看出观众对于这部剧的期待,总播放量达到50亿,可让人遗憾的是,豆瓣评分只有5.7,甚至都没有破6。
很多人会把这个剧和《庆余年》做对比,因为主创班底相同
400余万字的同名小说曾被捧为网文界里的“名著”,不少粉丝早就对跨界改编有所期待,剧版的主创班底、出品方与2019年的爆款剧《庆余年》相同则放大了这份期待。然而《雪中悍刀行》播出后,书粉的怒火蔓延到了各类讨论场合。
- requests模块的使用
- pandas保存表格数据
- pyecharts做词云图可视化
- python 3.8
- pycharm
- requests >>> pip install requests
- pyecharts >>> pip install pyecharts
import re import requests # 发送网络请求 import pandas as pd # 保存数据
headers = { 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36' } # 构建一个列表储存数据 data_list = [] for page in range(15, 1500, 30): url = f'https://mfm.video.qq.com/danmu?otype=json&target_id=7626435152%26vid%3Dp0041oidttf&session_key=0%2C174%2C1642248894×tamp={page}' esponse = requests.get(url=url, headers=headers)
json_data = response.json()
<Response [200]>: 告诉我们响应成功
comments = json_data['comments'] print(comments) for comment in comments: data_dict = {} data_dict['commentid'] = comment['commentid'] content = comment['content'] content = re.sub(r'[\x0e\xa0]', '', content) data_dict['content'] = content data_dict['opername'] = comment['opername'] print(data_dict) data_list.append(data_dict)
df = pd.DataFrame(data_list) # 乱码, 指定编码 为 utf-8 或者是 gbk 或者 utf-8-sig df.to_csv('data.csv', encoding='utf-8-sig')
import jieba from pyecharts.charts import WordCloud import pandas as pd from pyecharts import options as opts wordlist = [] data = pd.read_csv('data.csv')['content'] data a = [list(z) for z in zip(word, count)] c = ( WordCloud() .add('', a, word_size_range=[10, 50], shape='circle') .set_global_opts(title_opts=opts.TitleOpts(title="词云图")) ) c.render_notebook()