一分钟学会使用python代码批量下载B站视频(付源码下载链接)

项目场景:

在浏览B站视频时,遇到喜欢的视频,收藏后有与B站本身原因或者up主自身原因过段时间会失效,
今天呢,我来教大家怎么去在B站里面去下载视频。
`

解决方案:

复制BV号:

打开B站的视频链接地址
在这里插入图片描述

运行代码

在这里插入图片描述

查看结果

在这里插入图片描述

部分核心代码展示

import subprocess
import requests
import re
import pprint

headers = {
    'referer': 'https://www.bilibili.com/',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36'
}

def get_response(html_url):
    """发送请求函数"""
    response = requests.get(url=html_url, headers=headers)
    return response

def clean_title(title):
    """清理标题并提取文件名"""
    title = re.sub(r'[\/:*?"<>|]', '', title)  # 去除特殊字符
    punctuation_indices = [m.start() for m in re.finditer(r'[^\w\s]', title)]  # 查找所有标点符号
    if punctuation_indices:
        first_punct_index = punctuation_indices[0]
        if first_punct_index == 0 and len(punctuation_indices) > 1:
            first_punct_index = punctuation_indices[1]
        title = title[:first_punct_index]  # 取第一个标点符号前的内容
    title = title.strip()[:50]  # 限制文件名长度
    return title

def get_video_info(html_url):
    """获取视频标题 / CID / session"""
    response = get_response(html_url)
    cid = re.findall('"cid":(\d+),', response.text)[0]
    session = re.findall('"session":"(.*?)"', response.text)[0]
    title = re.findall('<h1[^>]+title="(.*?)"', response.text)[0].replace(' ', '')
    title = clean_title(title)  # 清理标题
    print(cid, session, title)
    video_info = [cid, session, title]
    return video_info

def get_video_content(cid, session, bv_id):
    index_url = 'https://api.bilibili.com/x/player/playurl'
    data = {
        'cid': cid,
        'qn': '0',
        'type': '',
        'otype': 'json',
        'fourk': '1',
        'bvid': bv_id,
        'fnver': '0',
        'fnval': '976',
        'session': session,
    }
    json_data = requests.get(url=index_url, params=data, headers=headers).json()
    audio_url = json_data['data']['dash']['audio'][0]['baseUrl']
    video_url = json_data['data']['dash']['video'][0]['baseUrl']
    print(audio_url, video_url)
    video_content = [audio_url, video_url]
    return video_content

def save(title, audio_url, video_url):
    """保存数据"""
    audio_content = get_response(audio_url).content
    video_content = get_response(video_url).content
    with open(title + '.mp3', mode='wb') as f:
        f.write(audio_content)
    with open(title + '.mp4', mode='wb') as f:
        f.write(video_content)
    print(title, '保存完成')

获取完整源码下载链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值