获取B站视频下载地址接口实现批量下载
简介
B站有很多值得下载的分P的资源,虽然bilibili前面加个i就可以进到下载页面,但是改动之后没办法用迅雷批量下载了,而且文件没办法命名为我们想要的标题,于是便有了这篇文章。最终效果如图:


思路
先看看平时是怎么下载的,现在B站的下载加入了一个QQ登录验证

不过刷新之后就没有弹出这个窗口了(虽然重启浏览器需要重新登录),因此只需要添加一个Cookies就可以解决这个登陆问题。可以参考之前的 Python实现电影排行榜自动网盘下载(4)Cookies免登录+抓包下载。

点击这个按钮之后便会出现真实的下载地址。拿Python来下载可能速度没有迅雷快,所以我想把批量获得的下载地址扔给迅雷下载,然后Python根据记录下的文件名批量重命名。
获取接口
点击之后抓到的包返回了真实的地址,

这个包的Request参数呢?

aid就是视频的编号,看看URL就知道啦;Page 第几个视频;最后一个_依次递增,和视频顺序无关。


直接用发包工具测试, 在线发包工具,添加上Cookies,不加最后一个参数(之前出现过参数有没有无所谓的情况)。也就是说,网址就直接这样扔过去了
http://api.bilibili.com/playurl?callback=cb&aid=13762839&page=1&platform=html5&quality=1&vtype=mp4&type=jsonp&cb=cb

哇,居然直接成功了!

……似乎有点太顺利了?

嗯,那就把这个URL拖出来去下载吧。果然有蹊跷,下不动。对比一下这种方法拿到的地址和正常下载的地址。

所以我们就还是要找到这最后一个参数才能成功下载。QAQ
这种参数一般Ctrl+Shift+F在文件中查找"_="
(注意要带上引号)


设下断点,重点 “下载手机视频”,果然断在了这里,而且就是我们要找的最后一个参数!

好吧,那就再来搜索vn这个参数!一直设断点找到它在哪里产生。然而我把所有找的vn都设了断点,再点下载按钮,发现并没有其他函数被断下来,还是直接就到了上图的代码位置,这是怎么回事?仿佛这个vn是凭空冒出来的一样。哇,莫不是加载页面的时候就生成了?按下F5,果然断在了其他的位置!


原来我们千辛万苦找的这个参数就是个时间函数啊!
既然是个时间函数,那我随便搞一个应该没问题吧?…我小心翼翼地下载了得到的地址…

……可以成功下载……
好气啊!你们怎么能这么随便啊喂!!!下面是获取接口这一段中间遇到的一些问题,也记录在这里啦
插曲(1)
之前出现过参数有没有无所谓的情况,所以我一开始测试的时候并没有加上最后一个参数,直接在POSTMAN里面扔过去。

我一开始还以为是这个参数什么的填错了,但是加上也不对?结果最后直接在浏览器里面输网址GET也成功了,

才发现,这丫的POSTMAN坑爹啊!它默认不支持Cookies,即使在Header里面加上Cookies段也无效…(
插曲(2)
找这个_参数来源于哪个JS文件。如下图Network-Initiator查找参数来源,从名字上我一眼就相中了这个函数。

这个index6.js倒是挺简单的,嘿嘿,就喜欢你这种傻白甜。注意到有一句输出

切换到Console看了一下,
http://upos-hz-mirrorks3.acgvideo.com/upgcxcode/64/36/22503664/22503664-1-16.mp4?um_deadline=1503804384&platform=html5&rate=106469&oi=1857879458&um_sign=916ec3367ba8228d4978f6ce04093fcf&gen=playurl&os=ks3
这TM不就是下载地址吗!!我天真的以为,这个地址可以在JS文件里找到生成方式,于是就来找这个j是怎么出来的,
Python实现

首先是批量获取下载链接,加上Cookie直接GET一个数据包,解析返回的JSON,然后在输出里面Ctrl+A Ctrl+C迅雷就自动侦测到视频地址啦。

然而下载的文件名是没有规律的,所以记录下这个文件名,下载完成后把这些文件名替换为我们需要的视频标题。所以任务包括以下几点:
- 获取视频数量(Page数量)
- 获取各个视频标题
- 记录原文件名对应的标题,并在下载完成后重命名
下载前的代码如下:
from urllib.request import *
import urllib.parse
import json
import re
from lxml import etree
avid='13762839'
logincookies=''
cookies=''
loginheaders = {
'Cookie':logincookies
}#登录验证的Cookie
downloadheaders={
'Cookie':cookies
}#下载验证的Cookie
pageurl='http://www.ibilibili.com/video/av'+avid
request=Request(pageurl,headers=loginheaders)
response=urlopen(request)
source=response.read()
html=etree.HTML(source)
titles=html.xpath('//*[@class="list-group-item"]')
pages=len(titles)
f=open('log.txt','w+')
for i in range(pages):
url='http://api.bilibili.com/playurl?callback=cb&aid={}&page={}&platform=html5&quality=1&vtype=mp4&type=jsonp&cb=cb&_=0'.format(avid,str(i+1))
request=Request(url,headers=downloadheaders)
response=urlopen(request)
htmlcode = (response.read().decode())
jsoncode=htmlcode[htmlcode.find('(')+1:htmlcode.find(')')]
restext=json.loads(jsoncode)
url=restext['durl'][0]['url']
print(url)
#这里不知道为什么最短匹配失效了,所以把原网址做了切分再进行正则搜索
pattern=re.compile("\/(.*?\.mp4)")
filename=re.findall(pattern,url[url.rfind('/'):])
# 井号作为分隔符
f.write('%s#%s\n'%(filename[0],titles[i].text.strip()+'.mp4'))
f.close()
生成了对应的文件记录log.txt


再运行重命名的程序
import os
import sys
with open('log.txt','r') as f:
data=f.readlines()
for filename in data:
filename=filename.replace('\n','')
origin,new=filename.split('#')
path=os.getcwd()+'\\downloads\\'
if os.path.isfile(path+origin):
os.rename(path+origin,path+new)

总结
- Cookies在同一个网页也有可能不同
- 以后看见参数是什么150开头的,就要开始怀疑它是当前时间,这种参数直接随便扔一个数字过去就行!
- 还有一个问题没解决…
http://upos-hz-mirrorks3.acgvideo.com/upgcxcode/37/19/20041937/20041937-1-16.mp4
用/
和.mp4
来匹配文件名,虽然用了最短匹配"\/(.*?\.mp4)"
但是输出却是upos-hz-mirrorks3.acgvideo.com/upgcxcode/37/19/20041937/20041937-1-16.mp4
这一点一直不知道是为什么。
两篇类似的抓包分析文章:
网易云音乐评论抓取实验(1)接口获取
Python实现电影排行榜自动网盘下载(4)Cookies免登录+抓包下载