Python爬虫案例分享

用Python采集网剧《猎罪图鉴》弹幕数据

2022-03-24  本文已影响0人  Python案例教学

前言

最近难得有一部剧能在其中保持着热度——檀健次和金世佳主演的单元探案剧《猎罪图鉴》。
该剧于2022年3月6日在首播
《猎罪图鉴》以悬疑题材独辟蹊径,成为这个初春的一匹黑马,该剧豆瓣14W人评分,目前为7.0分
今天就来采集一下它的弹幕数据

环境介绍

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.video.qq.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('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()
上一篇下一篇

猜你喜欢

热点阅读