Python实现获取弹幕的两种方式分享
作者:松鼠爱吃饼干 时间:2022-10-29 23:25:03
前言
弹幕可以给观众一种“实时互动”的错觉,虽然不同弹幕的发送时间有所区别,但是其只会在视频 * 定的一个时间点出现,因此在相同时刻发送的弹幕基本上也具有相同的主题,在参与评论时就会有与其他观众同时评论的错觉。
在国内的视频网站里,弹幕先是从A站被大家知道,随后B站发扬光大,导致现在全部视频平台和部分漫画平台都有弹幕功能,在欣赏动漫的同时,还能看一下大家的看法,也是一件非常有趣的事。
现在,弹幕文化成为了很多人看视频的习惯,今天就教大家如何获取弹幕的数据
环境
python 3.8
pycharm
requests
re
获取方式一: <简单, 但是弹幕很少>
先打开网站,找到你想要的视频,然后在网址bili前加个i,这样你就可以直接的找到弹幕的地址
复制地址打开,你就可以看到你想要的弹幕数据,写代码时直接请求这个地址就可以了
请求数据
url = 'https://api.bilibili.com/x/v1/dm/list.so?oid=967256583'
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.0.0 Safari/537.36'
}
response = requests.get(url=url, headers=headers)
print(response)
获取数据
response.encoding = 'utf-8'
print(response.text)
解析数据
content_list = re.findall('<d p=".*?">(.*?)</d>', response.text)
content = '\n'.join(content_list)
print(content_list)
保存数据
with open('方式一.txt', mode='a', encoding='utf-8') as f:
f.write(content)
获取方式二: <复杂一点点, 弹幕比较多,按日期来>
先回到视频播放地址,打开开发者工具,选择其他日期天数,然后会出现带有当天日期的数据包,右边就是我们要找的url地址
也出现了乱码的弹幕数据
请求数据
url = f'https://api.bilibili.com/x/v2/dm/web/history/seg.so?type=1&oid=967256583&date=2023-02-23'
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.0.0 Safari/537.36',
'cookie': '加自己的'
}
response = requests.get(url=url, headers=headers)
解析数据
content_list = re.findall('[\u4e00-\u9fa5]+', response.text)
content = '\n'.join(content_list)
翻页
for page in range(1, 24):
url = f'https://api.bilibili.com/x/v2/dm/web/history/seg.so?type=1&oid=967256583&date=2023-02-{page}'
保存数据
with open('方式二.txt', mode='a', encoding='utf-8') as f:
f.write(content)
print(content_list)
来源:https://pythonjx.blog.csdn.net/article/details/129209406
标签:Python,弹幕
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Vue2.0利用 v-model 实现组件props双向绑定的优美解决方案
2024-05-02 17:09:28
常用一些Javascript判断函数
2024-05-05 09:23:12
基于MySQL架构图解
2024-01-15 16:25:43
![](https://img.aspxhome.com/file/2023/2/84552_0s.png)
PHP mysql_result()函数使用方法
2023-06-13 08:21:29
如何利用Python写猜数字和字母的游戏
2021-01-28 00:00:09
![](https://img.aspxhome.com/file/2023/8/101258_0s.png)
pycharm如何使用anaconda中的各种包(操作步骤)
2021-10-18 05:35:11
![](https://img.aspxhome.com/file/2023/6/131326_0s.png)
详解Python AdaBoost算法的实现
2021-08-16 19:53:27
![](https://img.aspxhome.com/file/2023/3/66843_0s.jpg)
详解python列表(list)的使用技巧及高级操作
2021-05-12 15:26:47
大数据量,海量数据处理方法总结
2024-01-12 21:59:38
php简单防盗链验证实现方法
2023-09-12 04:55:15
python抓取网页图片示例(python爬虫)
2021-06-10 18:43:15
通过mysqladmin远程管理mysql的方法
2024-01-16 15:45:20
MySQL数据库定时任务举例讲解
2024-01-23 22:31:44
Python转换字典成为对象,可以用"."方式访问对象属性实例
2021-06-01 09:01:50
jQuery判断checkbox选中状态
2024-04-22 22:33:18
Python的bit_length函数来二进制的位数方法
2022-06-25 00:35:58
go语言实现猜数字小游戏的方法
2024-02-12 11:47:18
VSCode 配置uni-app的方法
2024-04-29 13:18:00
![](https://img.aspxhome.com/file/2023/7/132757_0s.png)
PIL对上传到Django的图片进行处理并保存的实例
2023-04-03 12:40:04
django的model操作汇整详解
2022-05-16 03:59:46
![](https://img.aspxhome.com/file/2023/8/120878_0s.png)