赞
踩
最近需要用到B站上的一些数据,例如弹幕和评论,无意间发现B站大佬在github开源了一个B站API调用库,使用文档详细,有人更新维护。
GitHub 地址为:https://github.com/Passkou/bilibili_api
目前已经更新到了v3.1.2版本。
通过这个 API 调用模块,我们可以实现获取视频的评论、弹幕、播放量等信息,还可以实现投币、点赞、发送弹幕等一系列的用户功能,非常方便。
在使用这个 API 之前,我们需要安装该库
pip install bilibili_api
此处我们抓取科技美学在2020-09-19发布的视频中的弹幕(方法中默认弹幕日期为当天,若需要历史弹幕,可以调用方法get_history_danmaku_index )
- from bilibili_api import video
- import re
- import pandas as pd
- #输出文件名
- file_name='科技美学.csv'
- #获取弹幕库
- danmu = video.get_danmaku(bvid="BV15T4y1c7Df")
- #正则表达式来处理数据
- data = [data.text for data in danmu]
- for i in data:
- i = re.sub('\s+', '', i)
- # 查看数量
- print("弹幕数量为:{}".format(len(data)))
- # 输出到文件
- df = pd.DataFrame(data)
- df.to_csv(file_name, index=False, header=None, encoding="utf_8_sig")
- print("写入文件成功")

赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。