【python爬虫】requests爬取BiliBili(b站)站视频
2020-06-01 05:10
357 查看
import requests import re import os class BiliBiliSpider(): def __init__(self, av_num): self.av_num = av_num self.url = 'https://www.bilibili.com/'+self.av_num self.headers = { 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36' } self.html_response = requests.get(url=self.url, headers=self.headers) def Downloads_videos(self): if self.html_response.status_code == 200: api_headers = { 'Range': 'bytes=0-999999999999999', 'Referer': 'https://www.bilibili.com/video/ShouHuWoMenZuiHaoDePiliPili', 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36' } html = self.html_response.text pattern = r'"base_url":"(.*?)"' urls = re.findall(pattern=pattern, string=html) if urls.__len__() == 0: pattern = r'"url":"(.*?)"' urls = re.findall(pattern=pattern, string=html) dir_path = './BiliBili_videos_'+self.av_num print('下载路径:'+dir_path) os.mkdir(dir_path) print('URL总数:{}'.format(urls.__len__())) for n in range(urls.__len__()): try: print('正在通过URL请求数据:'+urls[n]) with open(dir_path + '/video-' + str(n + 1) + '.mp4', 'wb') as f: with requests.get(url=urls[n], headers=api_headers, stream=True) as response: filesize = response.headers["Content-Length"] chunk_size = 128 for chunk in response.iter_content(chunk_size=chunk_size): f.write(chunk) print('写入完成,数据大小: {} bytes'.format(filesize)) except Exception as e: print(e) if __name__ == '__main__': BiliBiliSpider(av_num='av*********').Downloads_videos()
爬取的视频和音频是分离的,合并:
ffmpeg -i $1 -i $2 -vcodec copy -acodec copy $3
相关文章推荐
- Python爬虫 bilibili视频弹幕提取过程详解
- Python爬虫瞎玩系列(2)—— Bilibili视频最新投稿实时跟踪
- python爬虫——爬取b站APP视频信息(通过fiddler抓包工具)
- Python3 + selenium & requests 爬取B站视频实例(上)
- 初学Python爬虫:爬取B站前x个视频年份、投币数与播放量
- 【python爬虫】抓取B站视频相关信息(一)
- Python爬虫学习基础——5分钟学会爬取B站视频日播放量排行
- 【python爬虫】抓取B站视频数据及相关信息(二)
- Python爬虫之bilibili视频弹幕爬取并保存为JSON格式(详解分析+代码实现)
- python使用requests和re模块爬取bilibili高清视频
- Python3 + selenium & requests 爬取B站视频实例(下)
- Python 简单的方法爬取b站dnf视频封面
- [Python] - 爬虫之Requests基本使用
- 【Python爬虫实战--1】深入理解urllib;urllib2;requests
- python爬虫(实例1):requests+xpath 爬取并下载豆瓣电影的演员照片
- python淘宝爬虫基于requests抓取淘宝商品数据
- python手记(五):requests写爬虫(一):爬虫简介
- Python爬虫学习纪要(八):Requests 库学习笔记3
- 用python写网络爬虫-下载百思不得姐视频
- 分布式爬虫——爬取bilibili视频信息资源