Bilibili API 参考
约 1499 字大约 5 分钟
2026-03-19
IBiliAPIClient完整方法签名 — Bilibili 平台所有可用 API。源码:
ncatbot/api/bilibili/interface.py通过
api.bilibili.xxx()调用。
数据源管理
add_live_room()
async def add_live_room(self, room_id: int) -> None添加直播间监听。
remove_live_room()
async def remove_live_room(self, room_id: int) -> None移除直播间监听。
add_comment_watch()
async def add_comment_watch(
self, resource_id: str, resource_type: str = "video",
) -> None添加评论监听。
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| resource_id | str | — | 资源 ID(BV 号、动态 ID 等) |
| resource_type | str | "video" | 资源类型 |
remove_comment_watch()
async def remove_comment_watch(self, resource_id: str) -> None移除评论监听。
add_dynamic_watch()
async def add_dynamic_watch(self, uid: int) -> None添加动态监听。开始轮询指定用户的动态列表,检测到新动态或动态被删除时推送事件。
| 参数 | 类型 | 说明 |
|---|---|---|
| uid | int | 用户 UID |
remove_dynamic_watch()
async def remove_dynamic_watch(self, uid: int) -> None移除动态监听。
| 参数 | 类型 | 说明 |
|---|---|---|
| uid | int | 用户 UID |
add_dynamic_page_watch()
async def add_dynamic_page_watch(self, uid: int) -> None添加动态页监听。通过 Bilibili 动态主页接口一次性轮询多个关注用户的动态,效率优于为每个 UP 主单独建立 add_dynamic_watch。
添加前会先对目标 UID 执行关注操作(已关注或关注自己时自动跳过),确保动态主页包含该用户的动态。检测到新动态时推送 BiliDynamicEvent,不检测动态删除。
| 参数 | 类型 | 说明 |
|---|---|---|
| uid | int | 用户 UID |
remove_dynamic_page_watch()
async def remove_dynamic_page_watch(self, uid: int) -> None移除动态页监听。当所有订阅 UID 都移除时,动态页轮询自动停止。
| 参数 | 类型 | 说明 |
|---|---|---|
| uid | int | 用户 UID |
用户关系
follow_user()
async def follow_user(self, uid: int) -> Any关注用户。若已关注或目标为自己,静默返回 None 不抛出异常。
| 参数 | 类型 | 说明 |
|---|---|---|
| uid | int | 用户 UID |
list_sources()
async def list_sources(self) -> List[Dict[str, Any]]列出所有已注册的数据源。
直播间操作
send_danmu()
async def send_danmu(self, room_id: int, text: str) -> Any发送弹幕。
| 参数 | 类型 | 说明 |
|---|---|---|
| room_id | int | 直播间 ID |
| text | str | 弹幕内容 |
ban_user()
async def ban_user(self, room_id: int, user_id: int, hour: int = 1) -> Any禁言用户。
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| room_id | int | — | 直播间 ID |
| user_id | int | — | 被禁言的用户 ID |
| hour | int | 1 | 禁言时长(小时) |
unban_user()
async def unban_user(self, room_id: int, user_id: int) -> Any解除禁言。
set_room_silent()
async def set_room_silent(self, room_id: int, enable: bool, **kwargs: Any) -> Any全员禁言开关。
get_room_info()
async def get_room_info(self, room_id: int) -> Optional[LiveRoomInfo]获取直播间完整信息。网络异常时返回 None。
| 参数 | 类型 | 说明 |
|---|---|---|
| room_id | int | 直播间 ID |
返回值字段(LiveRoomInfo):
| 字段 | 类型 | 说明 |
|---|---|---|
room_info | RoomInfo | 直播间基础信息(标题、分区、状态、在线人数等) |
anchor_info | AnchorInfo | 主播信息(昵称、头像、直播等级等) |
watched_show | WatchedShow | 观看榜(人数、文本描述) |
完整字段定义见数据类型参考 → Bilibili 类型。
私信
send_private_msg()
async def send_private_msg(self, user_id: int, content: str) -> Any发送私信。
send_private_image()
async def send_private_image(self, user_id: int, image_url: str) -> Any发送私信图片。
get_session_history()
async def get_session_history(self, user_id: int, count: int = 20) -> list获取私信历史。
评论
send_comment()
async def send_comment(
self, resource_id: str, resource_type: str, text: str,
) -> Any发送评论。
reply_comment()
async def reply_comment(
self,
resource_id: str,
resource_type: str,
root_id: int,
parent_id: int,
text: str,
) -> Any回复评论。
| 参数 | 类型 | 说明 |
|---|---|---|
| resource_id | str | 资源 ID |
| resource_type | str | 资源类型 |
| root_id | int | 根评论 ID |
| parent_id | int | 被回复的评论 ID |
| text | str | 回复内容 |
delete_comment()
async def delete_comment(
self, resource_id: str, resource_type: str, comment_id: int,
) -> Any删除评论。
like_comment()
async def like_comment(
self, resource_id: str, resource_type: str, comment_id: int,
) -> Any点赞评论。
get_comments()
async def get_comments(
self, resource_id: str, resource_type: str, page: int = 1,
) -> list获取评论列表。
动态
get_user_dynamics()
async def get_user_dynamics(self, uid: int, offset: str = "") -> dict获取用户动态列表(新版接口)。
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| uid | int | — | 用户 UID |
| offset | str | "" | 分页偏移(上次返回的 offset) |
返回值:dict — 包含 items 列表的原始响应。每条动态包含 id_str、type、modules 结构。
get_user_latest_dynamic()
async def get_user_latest_dynamic(self, uid: int) -> Optional[dict]获取用户最新一条动态(按 pub_ts 取最大值)。
| 参数 | 类型 | 说明 |
|---|---|---|
| uid | int | 用户 UID |
返回值:Optional[dict] — 最新动态原始数据;无动态时返回 None。
用户查询
get_user_info()
async def get_user_info(self, user_id: int) -> dict获取用户信息。
视频查询
get_video_info()
async def get_video_info(self, video_id: str) -> Optional[VideoInfo]获取 B 站视频信息。
| 参数 | 类型 | 说明 |
|---|---|---|
| video_id | str | BV 号(如 BV1wa411Q7N6)或 av 号(如 av258296202) |
返回值:VideoInfo — 视频完整信息模型,解析失败时返回 None。
parse_bili_id()
async def parse_bili_id(self, text: str) -> Optional[ParsedBiliId]从文本中解析 B 站视频 ID。支持以下格式:
- b23.tv 短链:
https://b23.tv/xxxxxx(自动 HEAD 重定向获取真实 URL) - BV 号:文本中含
BV1xxxxxxxxx - av 号:文本中含
av12345
| 参数 | 类型 | 说明 |
|---|---|---|
| text | str | 待解析文本(可以是纯 ID、完整 URL 或包含链接的任意文本) |
返回值:ParsedBiliId — 解析结果,无法匹配时返回 None。
结果的 .video_id 属性可直接传入 get_video_info()。
参见 Bilibili 类型 § ParsedBiliId。
get_video_audio_url()
async def get_video_audio_url(
self, video_id: str, *, page_index: int = 0,
) -> Optional[str]获取视频音频流 URL。优先使用 DASH 模式获取独立音频流(体积小),失败时回退到 html5 FLV 合并流。
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
video_id | str | — | BV 号或 av 号 |
page_index | int | 0 | 分 P 索引(从 0 开始) |
返回值:Optional[str] — 音频流直链 URL;获取失败时返回 None。配合 AI 适配器的 transcription() 可实现视频语音转文字。
get_video_subtitle()
async def get_video_subtitle(
self, video_id: str, *, page_index: int = 0, language: str = "",
) -> Optional[str]获取视频字幕文本。自动下载字幕 JSON 并拼接为纯文本。
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
video_id | str | — | BV 号或 av 号 |
page_index | int | 0 | 分 P 索引 |
language | str | "" | 字幕语言(如 "zh-CN"),为空则取第一条可用字幕 |
返回值:Optional[str] — 字幕纯文本(各行以 \n 分隔);视频无字幕时返回 None。
推荐工作流
获取视频内容时,建议先尝试字幕,再回退 ASR。字幕质量更高且无需下载音频。
版权所有
版权归属:MI
