python爬虫:B站视频下载

发布于 2022-09-22  4 次阅读


教程

(暂无)

完整代码

# -*- coding : utf-8 -*-
import requests
import re
import json
import subprocess
import os
# 必须 "referer": "https://message.bilibili.com/"
headers = {
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36",
    "referer": "https://message.bilibili.com/"
}

def send_request(url):
    response = requests.get(url=url, headers=headers)
    return response

# 获取视频标题,视频链接,音频链接
def get_video_data(html_data):
    title = re.findall('<title data-vue-meta="true">(.*?)</title>',
                       html_data)[0].replace("_哔哩哔哩 (゜-゜)つロ 干杯~-bilibili", "")
    json_data = re.findall(
        r'<script>window.__playinfo__=(.*?)</script>', html_data)[0]
    json_data = json.loads(json_data)
    audio_url = json_data["data"]["dash"]["audio"][0]["backupUrl"][0]
    video_url = json_data["data"]["dash"]["video"][0]["backupUrl"][0]
    video_data = [title, audio_url, video_url]
    return video_data

# 下载
def save_data(file_name, audio_url, video_url):
    print("[+] 正在下载 " + file_name + "的音频...")
    audio_data = send_request(audio_url).content
    print("[+] 完成下载 " + file_name + "的音频!")
    print("[+] 正在下载 " + file_name + "的视频...")
    video_data = send_request(video_url).content
    print("[+] 完成下载 " + file_name + "的视频!")
    with open(file_name + ".mp3", "wb") as f:
        f.write(audio_data)
    with open(file_name + ".mp4", "wb") as f:
        f.write(video_data)

# 合并
def video_add_mp4(file_name, mp4_file):
    print("[+] 正在合并 " + mp4_file + "的视频...")
    outfile_name = file_name.split('.')[0] + '-new.mp4'
    # FFmpeg的位置
    cmd = f'E:\\FFmpeg\\bin\\ffmpeg -i {mp4_file} -i {file_name} -acodec copy -vcodec copy {outfile_name}'
    print(cmd)
    subprocess.call(cmd, shell=True)
    print("[+] 完成合并 " + mp4_file + "的视频!")
    print("[+] 清理mp3文件 " + file_name)
    os.remove(file_name)
    print("[+] 完成清理mp3文件:" + file_name)
    print("[+] 清理mp4文件:" + file_name)
    os.remove(mp4_file)
    print("[+] 完成清理mp4文件:" + file_name)

def main():
    url = input("输入bilibili视频对应的链接即可下载:")
    html_data = send_request(url).text
    video_data = get_video_data(html_data)
    save_data(video_data[0], video_data[1], video_data[2])
    print(video_data[0], video_data[1], video_data[2])
    # merge_data(video_data[0])
    a = video_data[0]+".mp3"
    b = video_data[0]+".mp4"
    video_add_mp4(a, b)

if __name__ == "__main__":
    main()

ffmpeg下载

链接:https://pan.baidu.com/s/1k_EdT-hXFnuCYU_sWrD2oQ
提取码:shpu


本当の声を響かせてよ