【bilibili】嗶哩嗶哩視頻下載

之前用的那個方式需要計算sign,雖說網上演算法已經爛大街了,但是加密用的key不是很多,而且我用的key是在git上偷的,所以就不發那個腳本了,發自己弄的一個。

準備:chrome, chromedriver, selenium(python3),之前的那個多線程下載器

鏈接:ChromeDriver Mirror 找對應版本:chromedriver與chrome各版本及下載地址 - CSDN博客 多線程下載器:【沒卵用】多線程下載器

下載好之後,把全部文件放在同一個文件夾內,然後就行了。

源碼:

# coding:utf-8from urllib import requestimport refrom selenium import webdriverimport jsonfrom selenium.webdriver.common.desired_capabilities import DesiredCapabilitiesimport ioimport gzipimport downimport osimport timedef dezip(source, *, codeform="utf-8"): if source.getheader("Content-Encoding") == "gzip": tmp = io.BytesIO(source.read()) dataunique = gzip.GzipFile(fileobj=tmp).read().decode(codeform) else: dataunique = source.read().decode(codeform) return datauniquefile = "E:\test\chrome"filetmp = file + "tmp"try: os.mkdir(filetmp)except FileExistsError: passheader = { "Connection": "keep-alive", "Accept": "application/json, text/javascript, */*; q=0.01", "Origin": "https://www.bilibili.com", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome" + "/63.0.3239.132 Safari/537.36", "Referer": "https://www.bilibili.com/video/av18420095/?spm_id_from=333.334.chief_recommend.22", "Accept-Encoding": "gzip, deflate, br", "Accept-Language": "zh-CN,zh;q=0.9"}url = ""d = DesiredCapabilities.CHROMEd[loggingPrefs] = {performance: ALL}sources = input("input video url or av number:")driver = webdriver.Chrome(".\chromedriver.exe", desired_capabilities=d)if sources.isdigit(): sourceurl = "https://www.bilibili.com/video/av" + sourceselse: sourceurl = sourcesdriver.get(sourceurl)# 如果獲取不到鏈接,請打開sleep(2)# time.sleep(2)for entry in driver.get_log(performance): try: a = re.findall("sign=", str(entry))[1] url = json.loads(entry["message"])["message"]["params"]["response"]["url"] break except IndexError: continuetitle = driver.title.replace(" ", "").replace("
", "").replace(".", "")driver.close()if url == "": print("wrong source url") raise RuntimeErrorprint("get url successfully")req = request.Request(url=url, headers=header)data = dezip(request.urlopen(req))print("get real url successfully")data = json.loads(data)history = open(filetmp + "data.txt", "w")if len(data["durl"]) > 1: for i in data["durl"]: down.download(i["url"], filetmp, str(i["order"]) + ".flv", header=header) history.write("file {0}{1}.flv
".format(filetmp, str(i["order"]))) history.close() if len(data["durl"]) > 1: os.system("ffmpeg -f concat -safe 0 -i {0} -c copy {1}.flv".format(filetmp + "data.txt", file + title)) for i in os.listdir(filetmp): os.remove(filetmp + i) os.removedirs(filetmp)else: down.download(data["durl"][0]["url"], file, title + ".flv", header=header)

食用方法:

av號:

輸入av號

鏈接方式:

注意是在播放界面

優點:管他喵的加密方式,反正不用用到;適用於所有嗶哩嗶哩視頻(包括番劇,使用鏈接方式);

bug:多p視頻只下載1p

推薦閱讀:

乾隆:其實我玩彈幕也沒多久,也就200多年吧
國內第一家彈幕視頻網,AcFun怎麼就離涼涼不遠了?

TAG:Python3x | 嗶哩嗶哩 |