基于Python3的B站视频下载器
B站真是越来越坑了,解析越来越难搞,抓到的链接也不能直接用,所以写了这个小程序,方便下载
#!/usr/bin/env python3
import sys
import ssl
import urllib.request
def report(count, blockSize, totalSize):
#下载进程显示
downloadedSize = count * blockSize
percent = int(downloadedSize * 100 / totalSize)
sys.stdout.write(f"\r已下载: {downloadedSize} B, 共: {totalSize} B, {percent} % 已完成")
sys.stdout.flush()
if __name__ == '__main__':
ssl._create_default_https_context = ssl._create_unverified_context
opener = urllib.request.build_opener()
opener.addheaders = [
('Host', '*****.acgvideo.com'), #抓到的链接前面的服务器地址
('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0'),
('Accept', '*/*'),
('Accept-Language', 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2'),
('Accept-Encoding', 'gzip, deflate, br'),
('Referer', 'https://www.bilibili.com/*****'), #播放网页的URL
('Origin', 'https://www.bilibili.com'),
('Connection', 'keep-alive'),
]
urllib.request.install_opener(opener)
url = 'https://catched.url.bilibili.server.acgvideo.com' #最重要的抓到的链接
urllib.request.urlretrieve(url, filename='*****.flv', reporthook=report) #保存的文件名