B站视频下载器
之前在B站查找学习资料时想批量下载,但是没找到合适的软件,就自己用python写了一个简易的,未打包,还有诸多不足,敬请指正(友情提示,不能拿来直接使用,需根据个人情况简单修改)
import os
import re
import requests
import time
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36',
'referer': 'https://www.bilibili.com/video/BV1oW4y1h7Py?p=67&vd_source=2f3cfb1ed7695b5dace415d1b1389506'
}
url_begin = input('请输入网址:')
url_1 = re.findall(r'https://www.bilibili.com/video/(.*?)p=\d+&vd_source=(.*?)', url_begin)
print(url_1)
# print(resp_begin)
a = int(input('从P几开始下载:'))
b = int(input('到P几结束:'))
for _ in range(a, b + 1):
url = ('https://www.bilibili.com/video/p=%s&vd_source= ' % _).replace('video/p=', 'video/' + url_1 + 'p=').replace('source= ', 'source=' + url_1 + ' ')
print(url)
response = requests.get(url, headers=headers)
response.encode = 'UTF-8'
# print(response.text)
# 提取名称
a = re.findall(r'cid":(\d+),"page":(\d+),"from":"vupload","part":"(.*?)","duration":', response.text)
b = re.findall(r'"codecid":\d+},\{"id":\d+,"baseUrl":"(.*?)","base_url"', response.text)
print(len(a), a, "\n", len(b), b)
i = len(b) - 1
# 获取视频和音频的二进制数据
resp_mp4 = requests.get(b, headers=headers).content
resp_mp3 = requests.get(b, headers=headers).content
# resp_b2 = requests.get(b, headers=headers).content
# resp_b8 = requests.get(b, headers=headers).content
# resp_b10 = requests.get(b, headers=headers).content
# resp_b11 = requests.get(b, headers=headers).content
# resp_b12 = requests.get(b, headers=headers).content
# 将获取的二进制数据写入文件.mp4/.mp3
with open(r"F:\IDM下载\Video\%s.mp4" % a, "wb") as f:
f.write(resp_mp4)
with open(r"F:\IDM下载\Video\%s.mp3" % a, "wb") as f:
f.write(resp_mp3)
# with open(r"F:\IDM下载\Video\%s2.mp3" % a, "wb") as f:
# f.write(resp_b2)
# with open(r"F:\IDM下载\Video\%s8.mp3" % a, "wb") as f:
# f.write(resp_b8)
# with open(r"F:\IDM下载\Video\%s10.mp3" % a, "wb") as f:
# f.write(resp_b10)
# 利用第三方软件ffmpeg将音频、视频合成到一起
os.system(
r'cd D:\ffmpeg-5.1-essentials_build\bin &ffmpeg -i "F:\IDM下载\Video\{1}.mp4" -i "F:\IDM下载\Video\{1}.mp3" -c copy "F:\IDM下载\申论\P{0}{1}.mp4"'.format(
a, a))
f.close()
time.sleep(2)
# 删除之前下载的mp4和mp3文件
# os.remove(r'F:\IDM下载\Video\{0}.mp4'.format(a))
# os.remove(r'F:\IDM下载\Video\{0}.mp3'.format(a))
print(a, '下载完毕')
测试一下看看 不错,要是打包一下就更好了{:1_921:} hyh666 发表于 2022-11-25 10:16
不错,要是打包一下就更好了
想打包来着,但是还得设置输出文件夹,感觉有点繁琐,就没做,代码修改方便 对小白可太不友好了
哪位大神打包下,谢谢
拿走测试一下,感谢楼主分享
cloud2010 发表于 2022-12-8 09:55
拿走测试一下,感谢楼主分享
需要根据实际情况简单修改 提示错误:IndexError: list index out of range 可能也灭与偶那么的
页:
[1]
2