python 下载bilibili视频
说明:
1.清晰度的选择要登录,暂时还没做,目前下载的视频清晰度都是默认的480P
2.进度条仿linux的,参考了一些博客修改了下,侵删
3.其他评论,弹幕之类的相关爬虫代码放在了https://github.com/teleJa/bilibili
4.判断sys.argv那个地方是因为一些爬虫调用了该文件,如果感觉不方面,直接传递视频番号进去就可以了
下载过程如图

直接上代码:
import requests
import re
import os
import json
import sys
import math
from lxml import etree class BLDSplider:
regex_cid = re.compile("\"cid\":(.{8})") def __init__(self, aid):
self.aid = aid self.origin_url = "https://www.bilibili.com/video/av{}?from=search&seid=9346373599622336536".format(aid)
self.headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36",
} self.url = "https://api.bilibili.com/x/player/playurl?avid={}&cid={}&qn=0&type=&otype=json" def check_dir(self, author_name):
# 检查目录
self.parent_path = "e:/bilibili/" + author_name + "/" + str(self.aid) + "/"
if not os.path.exists(self.parent_path):
os.makedirs(self.parent_path) self.video_name = self.parent_path + str(self.aid) + ".mp4" def parse_url(self, item):
cid = item["cid"]
print("aid:%s cid:%s" % (str(self.aid), cid))
title = item["title"]
print("title:%s" % title) self.headers["Referer"] = self.origin_url
# 视频
response = requests.get(self.url.format(self.aid, cid), headers=self.headers)
if response.status_code == 200:
result = json.loads(response.content.decode())
durl = result["data"]["durl"][0]
video_url = durl["url"]
print("video_url:%s" % video_url)
# 视频大小
size = durl["size"]
print("size:%s,约%2.2fMB" % (size, size / (1024 * 1024)))
video_response = requests.get(video_url, headers=self.headers, stream=True)
if video_response.status_code == 200:
with open(self.video_name, "wb") as file:
buffer = 1024
count = 0
while True:
if count + buffer <= size:
file.write(video_response.raw.read(buffer))
count += buffer
else:
file.write(video_response.raw.read(size % buffer))
count += size % buffer
file_size = os.path.getsize(self.video_name)
# print("\r下载进度 %.2f %%" % (count * 100 / size), end="") width = 50
percent = (count / size)
use_num = int(percent * width)
space_num = int(width - use_num)
percent = percent * 100
print('\r进度:[%s%s] %d%%' % (use_num * '#', space_num * ' ', percent), file=sys.stdout,
flush=True, end="")
if size == count:
break
print("\r\n") # 获取视频相关参数
def get_video_info(self):
response = requests.get(self.origin_url, headers=self.headers)
item = dict()
if response.status_code == 200:
# author
html_element = etree.HTML(response.content.decode())
author = dict()
author_name = html_element.xpath(
"/html/body/div[@id='app']/div[@class='v-wrap']/div[@class='r-con']/div[@id='v_upinfo']//a[@report-id='name']/text()")[
0]
# 通常是微博,微信公众号等联系方式
author_others = html_element.xpath(
"/html/body/div[@id='app']/div[@class='v-wrap']/div[@class='r-con']/div[@id='v_upinfo']//div[@class='desc']/@title")[
0]
author["name"] = author_name
author["others"] = author_others
item["author"] = author # cid
cid = BLDSplider.regex_cid.findall(response.content.decode())[0]
item["cid"] = cid
info_url = "https://api.bilibili.com/x/web-interface/view?aid={}&cid={}".format(self.aid, cid)
info_response = requests.get(info_url, headers=self.headers)
if info_response.status_code == 200:
data = json.loads(info_response.content.decode())["data"]
# 视频简介
desc = data["desc"]
item["desc"] = desc # title
title = data["title"]
item["title"] = title stat = data["stat"]
# 播放量
view = stat["view"]
item["view"] = view # 弹幕
danmaku = stat["danmaku"]
item["danmaku"] = danmaku # 评论
reply = stat["reply"]
item["reply"] = reply # 硬币
coin = stat["coin"]
item["coin"] = coin # 点赞
like = stat["like"]
item["like"] = like # 收藏
favorite = stat["favorite"]
item["favorite"] = favorite # 分享
share = stat["share"]
item["share"] = share
self.check_dir(item["author"]["name"])
# 视频参数
with open(self.parent_path + "video_info.txt", "w") as file:
file.write(json.dumps(item, ensure_ascii=False, indent=2))
return item def run(self):
item = self.get_video_info()
self.parse_url(item) def main():
#
aid = 55036734
if len(sys.argv) >= 2:
if sys.argv[1]:
aid = sys.argv[1]
splider = BLDSplider(aid)
splider.run() if __name__ == '__main__':
main()

python 下载bilibili视频的更多相关文章
- Python 批量下载BiliBili视频 打包成软件
文章目录 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很多已经做案例的人,却不知道如何去学习更加高深的知识.那么针对这三类人,我给大家 ...
- 如何下载Bilibili视频
方法1: https://www.bilibili.com/video/av25940642 (源网址) https://www.ibilibili.com/video/av25940642 (新网址 ...
- python下载youtube视频
谷歌开源了一个新的数据集,BoundingBox,(网址在这里)这个数据集是经过人工标注的视频数据集,自然想将它尽快地运用在实际之中,那么首先需要将其下载下来:可以看到网址上给出的是csv文件,该文件 ...
- 爬虫 | Python下载m3u8视频
目录 从 m3u8 文件中解析出 ts 信息 按时间截取视频 抓取 ts 文件 单文件测试 批量下载 合并 ts 文件 将合并的ts文件转化为视频文件 参考资料: m3u8格式介绍 ts文件格式介绍 ...
- python下载网页视频
因网站不同需要修改. 下载 mp4 连接 from bs4 import BeautifulSoup import requests import urllib import re import js ...
- 下载bilibili视频
http://www.urlgot.com/zh_CN/
- 利用Selenium和Browsermob批量嗅探下载Bilibili网站视频
Rerence: http://www.liuhao.me/2016/09/20/selenium_browsermob_sniff_bilibili_video/ 日常生活中,用电脑看视频是非常频繁 ...
- Go设计模式学习准备——下载bilibili合集视频
需求 前段时间面试,被问到设计模式.说实话虽然了解面向对象.多态,但突然被问到设计模式,还要说清解决什么问题,自己是有些懵的,毕竟实习主要工作是在原项目基础进行CRUD,自己还是没有深度思考,所以只能 ...
- Python:使用youtube-dl+ffmpeg+FQ软件下载youtube视频
声明:本文所述内容都是从http://blog.csdn.net/u011475134/article/details/71023612博文中学习而来. 背景: 一同学想通过FQ软件下载一些youtu ...
随机推荐
- SSH applicationContext.xml import异常
近期在项目上,遇到了一个问题.在配置applicationContext.xml使用<import>标签引入其他的xml文件时,导致项目启动时过慢.有时还会引起启动异常.后来查到是xml文 ...
- Azkaban3.x
Azkaban3.x安装部署 官方文档地址 三种模式 solo-server模式:exec进程和web进程为同一个进程,存放元数据的数据库为H2 two-server模式:与之前的单机版本类似,exe ...
- vue插件大全
一.UI组件及框架 element - 饿了么出品的Vue2的web UI工具套件 mint-ui - Vue 2的移动UI元素 iview - 基于 Vuejs 的开源 UI 组件库 Keen-UI ...
- 1<<33这种写法是错的!!!
1<<33不能这么写,1默认int类型,应该改为(long long)1<<33
- Celery后台任务
Celery 在程序运行过程中,经常会遇到一些耗时耗资源的任务,为了避免这些任务阻塞主进程的运行,我们会采用多线程或异步任务去处理.比如在Web中需要对新注册的用户发一封激活邮件来验证账户,而发邮件本 ...
- HDOJ1016 Prime Ring Problem(DFS深层理解)
Prime Ring Problem 时间限制: 200 ...
- url地址栏参数<==>对象(将对象转换成地址栏的参数以及将地址栏的参数转换为对象)的实用函数
/** * @author web得胜 * @param {Object} obj 需要拼接的参数对象 * @return {String} * */ function obj2qs(obj) { i ...
- Spark JDBC系列--Mysql tinyInt字段特殊处理
当spark取出表的scheme中,类型名为tinyint的字段,会被处理为Boolean型.而mysql中tinyint的sqlType都会默认处理为bit,所以如果数据库中的这类字段中,存储了0. ...
- H3C 主要局域网技术
- PyTorch之前向传播函数自动调用forward
参考:1. pytorch学习笔记(九):PyTorch结构介绍 2.pytorch学习笔记(七):pytorch hook 和 关于pytorch backward过程的理解 3.Pytorch入门 ...