Python下载b站视频
import subprocess from pprint import pprint import requests import re, json url = 'https://www.bilibili.com/video/BV1JL411G7u1?spm_id_from=333.999.0.0' headers = { 'referer': 'https://www.bilibili.com/', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36' } res = requests.get(url, headers=headers) # print(res.text) html = res.text # 标题 # title = re.findall('title="(.*?)" class="video-title">', html) title = re.findall('"true">(.*?)_哔哩哔哩', html)[0] str_data = re.findall('__playinfo__=(.*?)</script>', html)[0] # print(html) # print("---------------") # # print(title) # # print("---------------") data = json.loads(str_data) # pprint(data) audio_url = data['data']['dash']['audio'][0]['backupUrl'][0] video_url = data['data']['dash']['video'][0]['backupUrl'][0] print(audio_url) print(video_url) audio_content = requests.get(audio_url, headers=headers).content video_content = requests.get(video_url, headers=headers).content with open(f'./{title}.mp3', 'ab') as f: f.write(audio_content) with open(f'./{title}.mp4', 'ab') as f: f.write(video_content) # 合并音频和视频 需要ffmpeg 并添加环境变量 cmd = f"ffmpeg -i {title}.mp4 -i {title}.mp3 -c:v copy -c:a aac -strict experimental {title}out.mp4" subprocess.run(cmd, shell=True)
人生苦短,慢慢潇洒。
nsyw.run