弹幕全是“二刷”,这部剧有多好看?Python爬取腾讯视频1.1W弹幕,做词云分析

《猎罪图鉴》可以说是国产悬疑剧之光了,上线首周热度不断飙升。
该剧讲述了因一起尘封旧案而结怨的模拟画像师沈翊和刑警队长杜城,在机缘巧合下被迫搭档,两人联手侦破多起离奇疑案,共同追踪谜底真相的故事。
今天就用Python爬取该剧弹幕,做词云图

环境介绍

python 3.8
pycharm
requests >>> pip install requests
pyecharts >>> pip install pyecharts

请求数据

headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Safari/537.36'
}
for page in range(15, 1500, 30):
    url = f'https://mfm.XXXX.com/danmu?otype=json&target_id=7712618480%26vid%3Dg00423lkmas&session_key=0%2C0%2C0&timestamp={page}&_=1647931110703'

    response = requests.get(url=url, headers=headers)

获取数据 从一个字符串 变成了一个 字典 (容器)

json_data = response.json()

解析数据

for comment in json_data['comments']:
    commentid = comment['commentid']
    opername = comment['opername']
    content = comment['content']

保存数据

with open('弹幕.csv', encoding='utf-8-sig', mode='a', newline='') as f:
    csv_writer = csv.writer(f)
    csv_writer.writerow([commentid, opername, content])

运行代码,得到1W多条弹幕数据

词云可视化

导入数据

wordlist = []
data = pd.read_csv('弹幕.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()

posted @ 2022-03-29 22:51  松鼠爱吃饼干  阅读(134)  评论(0编辑  收藏  举报
Title