【python】bilibili视频下载器
本帖最后由 未晴雾香 于 2022-1-14 16:45 编辑# BiBiDownload
download bilibili video
## 更改cookie
!(https://cdn.jsdelivr.net/gh/lnyo-cly/blogImg/pics/20220114162933.png)
!(https://cdn.jsdelivr.net/gh/lnyo-cly/blogImg/pics/20220114162954.png)
## 获取视频数据
!(https://cdn.jsdelivr.net/gh/lnyo-cly/blogImg/pics/20220114162129.png)
## 打印视频数据
!(https://cdn.jsdelivr.net/gh/lnyo-cly/blogImg/pics/20220114162250.png)
## 下载视频
直接回车即为默认
!(https://cdn.jsdelivr.net/gh/lnyo-cly/blogImg/pics/20220114162336.png)
!(https://cdn.jsdelivr.net/gh/lnyo-cly/blogImg/pics/20220114162444.png)
## 画质调整
> 1080P和以上需要登录,1080p60+需要大会员
修改qn值:
"高清 1080P60"---116,
"高清 1080P"-----80,
"高清 720P"------64,
"清晰 480P"------32,
"流畅 360P"------16
!(https://cdn.jsdelivr.net/gh/lnyo-cly/blogImg/pics/20220114163054.png)
import requests
import json
import pprint
import re
from tqdm import tqdm
import time
import os
session = requests.session()
ico={}
videoInfos=[]
#cookie登录账号
def cookieLogin():
url="http://api.bilibili.com/x/space/myinfo"
headers={
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36 Edg/97.0.1072.55",
"cookie":"SESSDATA=更改此处"
}
res=session.get(url=url,headers=headers).text
myJSON=json.loads(res)
userName=myJSON["data"]["name"]
vipStatus=myJSON["data"]["vip"]["status"]
if vipStatus==0:
vipStatus="非会员"
else:
vipStatus="大会员"
#pprint.pprint(myJSON)
print("您的账号名:"+userName+",当前为:"+vipStatus)
#获取视频数据
def getVideDate(bv):
url="https://api.bilibili.com/x/web-interface/view?bvid="+bv
headers={
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36 Edg/97.0.1072.55",
"cookie":"SESSDATA=更改此处"
}
viderJSON=json.loads(session.get(url=url,headers=headers).text)
ico["video"]=viderJSON["data"]["pic"]
ico["up"]=viderJSON["data"]["owner"]["face"]
video=viderJSON["data"]["pages"]
print("正在获取视频数据\n视频标题:"+viderJSON["data"]["title"])
for i in tqdm(range(len(video))):
url="http://api.bilibili.com/x/player/playurl?cid={cid}&bvid={bv}&qn={qn}".format(cid=video["cid"],bv=bv,qn=80)
res=session.get(url=url,headers=headers).text
res=json.loads(res)
videoInfo={}
videoInfo["url"]=res["data"]["durl"]["url"]
videoInfo["title"]=video["part"]
if res["data"]["quality"]==116:
videoInfo["quality"]="高清 1080P60"
elif res["data"]["quality"]==80:
videoInfo["quality"]="高清 1080P"
elifres["data"]["quality"]==64:
videoInfo["quality"]="高清 720P"
elifres["data"]["quality"]==32:
videoInfo["quality"]="清晰 480P"
elifres["data"]["quality"]==16:
videoInfo["quality"]="流畅 360P"
global videoInfos
videoInfos.append(videoInfo)
#print(i,url,video["part"])
#print(i,videoInfo)
time.sleep(0.2)
#pprint.pprint(videoInfos)
print("视频数据获取完成\n")
#打印视频数据
def printVideoDate():
if videoInfos==[]:
print("未找到视频数据,请先获取视频数据!")
return
for i in range(len(videoInfos)):
video=videoInfos
print(str(i+1)+":"+video["title"]+" "+video["quality"])
#下载视频
def downVideo(dir,num):
# 文件夹不存在,则创建文件夹
folder = os.path.exists(dir)
#print(os.getcwd())
if not folder:
os.makedirs(dir)
url=videoInfos["url"]
videoName=videoInfos["title"]+" "+videoInfos["quality"]
headers={
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36 Edg/97.0.1072.55",
"referer":"https://www.bilibili.com"
}
res=session.get(url=url,headers=headers,stream=True)
total_size = int(int(res.headers["Content-Length"])/1024+0.5)
with open(dir+videoName+".flv","wb") as f:
print('开始下载文件:{},当前文件大小:{}KB'.format(videoName,total_size))
for chunk in tqdm(res.iter_content(1024),total=total_size,unit='k',desc='Downloading'):
f.write(chunk)
print(videoName,"下载完毕")
def showMenu():
print("0: 显示菜单\n1: 获取视频数据\n2: 打印视频数据\n3: 下载视频\n-1: 退出程序")
if __name__=="__main__":
cookieLogin()
print()
showMenu()
# bv="BV1eT4y1Z7T7"
while(True):
i=input("请输入功能菜单编号:")
if i=='-1':
break;
elif i=='0':
showMenu()
elif i=='1':
bv=input("请输入BV号:")
getVideDate(bv)
printVideoDate()
elif i=='2':
printVideoDate()
elif i=='3':
num=input("请输入要下载的编号(多个视频写成数组形式,例如:),默认为下载所有视频:")
dir=input("请输入保存路径,默认为./download/:")
if dir=='':
dir="./download/"
if(num==''):
print("预下载视频"+str(len(videoInfos))+'个')
for i in range(len(videoInfos)):
downVideo(dir=dir,num=i)
elif(isinstance(eval(num),int)):
downVideo(dir=dir,num=int(num)-1)
elif(isinstance(eval(num),list)):
print("预下载视频"+str(len(eval(num)))+'个')
for i in eval(num):
downVideo(dir=dir,num=i-1)
elif i=='':
pass
print("退出成功")
GitHub项目地址:https://github.com/LnYo-Cly/BiBiDownload
顶一下,改了点东西,就是在文件名前加了 P 代表影片排序,否则下下来有可能是乱序的
# 如果一个影片中有多个P,就带上P,其实1个话,也带上了。
videoInfo["title"] = 'P' + str(video["page"]) + ' ' + video["part"] 感谢分享 ,py学过一段时间就放弃了{:1_907:} 来了学习下
希望楼主弄个成品给小白们 感谢分享,学习学习 谁有好用的微信视频提取 谢谢楼主的分享,要是楼主搞个多线程下载就好了,现在平均下载速度就1M/s,还是有点慢的 小姐姐的男朋友 发表于 2022-1-15 21:34
谢谢楼主的分享,要是楼主搞个多线程下载就好了,现在平均下载速度就1M/s,还是有点慢的
hh,目前还不会写python的多线程{:1_909:} 本帖最后由 Ron2011 于 2022-1-18 11:08 编辑
学习了,非常感谢,问个小白问题,问什么我运行报错?
File "zmq/backend/cython/checkrc.pxd", line 19, in zmq.backend.cython.checkrc._check_rc
Again: Resource temporarily unavailable