import os
import os.path
import you_get
from lxml import etree
import requests
# 获取所有的视频url
def get_url(ID):
video_url = 'https://www.bilibili.com/video/'
# ID = 'BV1Kh411r7uR'
home_url = video_url + ID
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36'
}
response = requests.get(home_url, headers=headers)
response.encoding = 'utf-8'
text = response.text
html = etree.HTML(text)
li_list = html.xpath('//div[@]/ul/li')
print(len(li_list))
url_list = []
for i in range(1, len(li_list) + 1):
url = '{}{}?p={}'.format(video_url, ID, i)
url_list.append(url)
print(url)
return url_list
# 检测
def detection(ID, name):
status = True
try:
# 获取所有的视频url
url_list = get_url(ID)
# 统计共有多少个视频
num = len(url_list)
# 展示一共多少个视频
print('共有{}个视频'.format(num))
except:
print('获取视频失败')
url_list = []
status = False
# 判断用户是否输入内容
if name:
# 获取当前工作目录
path = os.getcwd()
# 拼接文件目录
file_path = os.path.join(path, name)
if os.path.isdir(file_path):
print('该文件已存在,请重新命名')
status = False
else:
print('文件夹名称没问题')
else:
print('请输入创建文件夹的名称')
status = False
return status, url_list
# 开始批量下载b站视频
def Start():
ID = input('请输入视频ID:')
name = input('请输入文件夹名称')
result, url_list = detection(ID, name)
if result == True:
# 创建保存视频的文件夹
os.mkdir(name)
# 获取当前工作目录
path = os.getcwd()
# 拼接文件目录
video_path = os.path.join(path, name)
# 切换到保存视频的文件夹
os.chdir(video_path)
p = 1
# 下载视频
for url in url_list:
os.system('you-get {}'.format(url))
print('已下载{}个视频'.format(p))
p += 1
print('已完成')
else:
print('出问题了,请重新运行!')
Start()
使用及效果:
运行代码后,在终端中输入视频的ID号,保存视频的文件夹名称,如果没有问题,就会开始下载,如果有问题就重新运行,重新输入内容
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)