弹幕全是“二刷”,这部剧有多好看?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×tamp={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()

Original: https://www.cnblogs.com/qshhl/p/16074292.html
Author: 松鼠爱吃饼干
Title: 弹幕全是“二刷”,这部剧有多好看?Python爬取腾讯视频1.1W弹幕,做词云分析

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/500180/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球