如何使用Python下载哔哩哔哩(Bilibili)视频字幕

在本文中,我将向大家展示如何使用Python下载哔哩哔哩(Bilibili)视频的字幕。通过这个方法,你可以轻松地获取你喜欢的视频的字幕文件,方便学习和交流。

准备工作

在开始之前,我们需要安装一些必要的库,确保你已经安装了requestsjson库,如果没有安装,可以使用以下命令安装:

复制代码
pip install requests

下载字幕

下面是一个Python脚本,可以帮助你下载指定哔哩哔哩视频的字幕。你只需要将视频的BV号(BV号是Bilibili视频的唯一标识符)替换到代码中的BVID变量中,运行脚本即可下载字幕文件。

python 复制代码
"""下载哔哩哔哩 字幕
"""
import math
import os
import time
import requests
import json



def download_subtitle_json(bvid: str):
    """
    下载字幕
    """
    sub_dir = f'./{bvid}'
    if not os.path.isdir(sub_dir):
        os.mkdir(f'./{bvid}')
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:125.0) Gecko/20100101 Firefox/125.0',
        'Accept': 'application/json, text/plain, */*',
        'Accept-Language': 'en-US,en;q=0.5',
        'Referer': f'https://www.bilibili.com/video/{bvid}/?p=1',
        'Origin': 'https://www.bilibili.com',
        'Connection': 'keep-alive',
        'Cookie': "xxxxxxxx",
        'Sec-Fetch-Dest': 'empty',
        'Sec-Fetch-Mode': 'cors',
        'Sec-Fetch-Site': 'same-site',
    }
    resp = requests.get(f'https://www.bilibili.com/video/{bvid}/', headers=headers)
    text = resp.text
    aid = text[text.find('"aid"') + 6:]
    aid = aid[:aid.find(',')]
    cid_back = requests.get("http://api.bilibili.com/x/player/pagelist?bvid={}".format(bvid), headers=headers)
    if cid_back.status_code != 200:
        print('获取 playlist 失败')

    cid_json = json.loads(cid_back.content)
    for item in cid_json['data']:
        cid = item['cid']
        title = item['part'] + '.json'

        params = {
            'aid': aid,
            'cid': cid,
            'isGaiaAvoided': 'false',
            'web_location': '1315873',
            'w_rid': '364cdf378b75ef6a0cee77484ce29dbb',
            'wts': int(time.time()),
        }

        wbi_resp = requests.get('https://api.bilibili.com/x/player/wbi/v2', params=params, headers=headers)
        if wbi_resp.status_code != 200:
            print('获取 字幕链接 失败')
        subtitle_links = wbi_resp.json()['data']["subtitle"]['subtitles']
        if subtitle_links:
            # 默认下载第一个字幕
            subtitle_url = "https:" + subtitle_links[0]['subtitle_url']
            subtitle_resp = requests.get(subtitle_url, headers=headers)
            open(os.path.join(sub_dir, title), 'w', encoding='utf-8').write(subtitle_resp.text)


if __name__ == '__main__':
    BVID = 'BV11T42117FY'
    download_subtitle_json(BVID)
python 复制代码
# 请将视频的BV号替换为你想要下载字幕的视频BV号
BVID = 'BV11T42117FY'
download_subtitle_json(BVID)

运行结果:

感谢大家的阅读!如果你有任何问题或建议,欢迎在评论区留言。谢谢!

相关推荐
天启HTTP16 小时前
爬虫频繁弹验证码?解析网站反爬检测机制
网络·爬虫·tcp/ip
小猴子爱上树18 小时前
跨境图片翻译工具,批量处理商品图视频字幕
python·音视频
2301_8059629318 小时前
在昉星光2(RISC-V)上构建每日短视频自动生成器
音视频·risc-v
xyz_CDragon19 小时前
MiniMax H3 vs Seedance 2.5 vs Kling 3.0:2026 AI视频生成模型横评(排行榜+价格+开源实测)
人工智能·开源·音视频·transformer·minmax h3
程序员老陆19 小时前
FFmpeg libswresample 模块的关键函数swr_convert 到底在干嘛?
ffmpeg·音视频·格式转换·音频重采样
深蓝电商API19 小时前
如何快速定位加密函数?
爬虫·加密函数
csdnfanguyinheng20 小时前
端到端加密音视频通话系统
音视频
oh,huoyuyan1 天前
网页公开数据采集工具推荐
人工智能·爬虫·火车采集器
show4331 天前
2026小程序端视频转文字技术对比:识别引擎精度实测
小程序·音视频
崔子末1 天前
某影视库剧集列表以及查询接口逆向
爬虫·python