好友
阅读权限10
听众
最后登录1970-1-1
|
之前在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[0][0] + 'p=').replace('source= ', 'source=' + url_1[0][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[_ - 1], "\n", len(b), b)
i = len(b) - 1
# 获取视频和音频的二进制数据
resp_mp4 = requests.get(b[0], headers=headers).content
resp_mp3 = requests.get(b, headers=headers).content
# resp_b2 = requests.get(b[2], headers=headers).content
# resp_b8 = requests.get(b[8], headers=headers).content
# resp_b10 = requests.get(b[10], headers=headers).content
# resp_b11 = requests.get(b[11], headers=headers).content
# resp_b12 = requests.get(b[12], headers=headers).content
# 将获取的二进制数据写入文件.mp4/.mp3
with open(r"F:\IDM下载\Video\%s.mp4" % a[_ - 1][2], "wb") as f:
f.write(resp_mp4)
with open(r"F:\IDM下载\Video\%s.mp3" % a[_ - 1][2], "wb") as f:
f.write(resp_mp3)
# with open(r"F:\IDM下载\Video\%s2.mp3" % a[_ - 1][2], "wb") as f:
# f.write(resp_b2)
# with open(r"F:\IDM下载\Video\%s8.mp3" % a[_ - 1][2], "wb") as f:
# f.write(resp_b8)
# with open(r"F:\IDM下载\Video\%s10.mp3" % a[_ - 1][2], "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[_ - 1][1], a[_ - 1][2]))
f.close()
time.sleep(2)
# 删除之前下载的mp4和mp3文件
# os.remove(r'F:\IDM下载\Video\{0}.mp4'.format(a[_ - 1][2]))
# os.remove(r'F:\IDM下载\Video\{0}.mp3'.format(a[_ - 1][2]))
print(a[_ - 1][2], '下载完毕')
|
|
发帖前要善用【论坛搜索】功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。 |
|
|
|
|