from tkinter.ttk import *
import os
from tkinter.filedialog import askdirectory
import tkinter as tk
import re
import requests
import time
def mkdir(path): # 定义在桌面生成文件夹
去除首位空格
path = path.strip()
# 去除尾部 \ 符号
path = path.rstrip("\\")
# 判断路径是否存在 # 存在 True # 不存在 False
isexists = os.path.exists(path)
# 判断结果
if not isexists:
# 如果不存在则创建目录
# 创建目录操作函数
os.makedirs(path)
print(path + ' 创建成功')
return True
else:
# 如果目录存在则不创建,并提示目录已存在
print(path + ' 目录已存在')
return False
# 定义要创建的目录
创建文件夹
pqpath = r"F:\B站视频爬取工作目录\Video"
调用函数
mkdir(pqpath)
window = tk.Tk() # 创建窗口
window.title("B站视频爬取")
window.geometry("282x130+100+200")
window.resizable(width=False, height=False)
label_1 = tk.Label(text="请输入网址:", anchor="center")
label_1.place(x=0, y=0, width=79, height=24)
ipt_1 = tk.Entry()
ipt_1.place(x=80, y=0, width=198, height=25)
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'
}
label_2 = tk.Label(text="下载P", anchor="center")
label_2.place(x=0, y=30, width=50, height=24)
m = int()
url_1 = ''
def begin_p():
global m, url_1
url_begin = ipt_1.get()
url_1 = re.findall(r'https://www.bilibili.com/video/(.*?)p=\d+&vd_source=(.?)', url_begin)
response = requests.get(url_begin, headers=headers)
response.encode = 'UTF-8'
m = int(re.findall(r'"bvid":.?,"aid":.?,"videos":(.?),"tid":.*?,"tname":', response.text)[0])
print(url_1, m)
cb_1['values'] = list(range(1, m + 1))
cb_1 = Combobox(window, state="readonly", postcommand=begin_p)
cb_1['values'] = ()
cb_1.place(x=50, y=30, width=100, height=26)
label_3 = tk.Label(text="至P", anchor="center")
label_3.place(x=150, y=30, width=28, height=24)
def finall_p():
global m
n = int(cb_1.get())
cb_2['values'] = list(range(n, m + 1))
cb_2 = Combobox(window, state="readonly", postcommand=finall_p)
cb_2['values'] = ()
cb_2.place(x=180, y=30, width=101, height=26)
var_1 = ''
def xzpath():
global var_1
var_1 = askdirectory()
btn_1.configure(text="下载至" + var_1)
print("下载至", var_1)
btn_1 = tk.Button(text="请选择下载文件夹", command=xzpath)
btn_1.place(x=-1, y=60, width=281, height=27)
def xz():
import os
global url_1, var_1
a = int(cb_1.get())
b = int(cb2.get())
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[i], headers=headers).content
# 将获取的二进制数据写入文件.mp4/.mp3
with open(r"F:\B站视频爬取工作目录\Video\%s.mp4" % a[_ - 1][2], "wb") as f:
f.write(resp_mp4)
with open(r"F:\B站视频爬取工作目录\Video\%s.mp3" % a[_ - 1][2], "wb") as f:
f.write(resp_mp3)
# 利用第三方软件ffmpeg将音频、视频合成到一起
f.close()
# print('"F:\B站视频爬取工作目录\Video\{0}.mp4", "F:\B站视频爬取工作目录\Video\{0}.mp3", "{1}\{0}.mp4"'.format(a[_ - 1][2], var_1))
os.system(
r'cd D:\ffmpeg-5.1-essentials_build\bin &ffmpeg -i "F:\B站视频爬取工作目录\Video\{1}.mp4" -i "F:\B站视频爬取工作目录\Video\{1}.mp3" -c copy "{0}\{1}.mp4"'.format(
var_1, a[_ - 1][2]))
time.sleep(2)
# 删除之前下载的mp4和mp3文件
# os.remove(r'F:\B站视频爬取工作目录\Video\{0}.mp4'.format(a[_ - 1][2]))
# os.remove(r'F:\B站视频爬取工作目录\Video\{0}.mp3'.format(a[_ - 1][2]))
print(a[_ - 1][2], '下载完毕')
btn_2 = tk.Button(text="开始下载", command=xz)
btn_2.place(x=0, y=90, width=280, height=39)
window.mainloop()