爬B站视频.PY

import json
import re
from concurrent.futures import ThreadPoolExecutor, wait, ALL_COMPLETED

import requests
from tqdm import tqdm


def get_response(html_url, stream=False):
    headers = {
        "referer": "https://www.bilibili.com/",
        "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36"
    }
    response = requests.get(html_url, headers=headers, stream=stream)
    return response


def get_url(html_url):
    r = requests.get(html_url)
    a = re.search(r"window\.__INITIAL_STATE__=(.*?)};", r.text).group(1)
    json_data = json.loads(a + "}")
    video_data = json_data['videoData']
    avid = str(video_data['aid'])
    qn = '112'
    urls = []

    for p in video_data['pages']:
        cid = str(p['cid'])
        title = p['part']
        if len(video_data['pages']) == 1:
            title = video_data['title']
        urls.append({
            'title': title,
            "url": "https://api.bilibili.com/x/player/playurl?cid=" + cid + "&avid=" + avid + "&an=" + qn + "&otype=json&fourk=1"
        })
    return urls


def save(video_url, video_size, title):
    video_res = get_response(video_url, True)
    with open(r'D:\crawFile\bili\\'+title + '.mp4', 'wb') as fd:
        print('开始下载文件:{}, 当前文件大小:{}KB'.format(title, video_size / 1024))
        for c in tqdm(iterable=video_res.iter_content(), total=video_size, unit='b', desc=None):
            fd.write(c)
        print('{},文件下载成功'.format(title))


def a_single_download(info):
    B站 = get_response(info['url'])
    json_data = json.loads(B站.text)
    video_url = json_data['data']['durl'][0]['url']
    video_size = json_data['data']['durl'][0]['size']
    save(video_url, video_size, info['title'])


def concurrent_download(base_infos):
    executor = ThreadPoolExecutor(max_workers=10)
    futur_tasks = [executor.submit(a_single_download, info) for info in base_infos]
    wait(futur_tasks, return_when=ALL_COMPLETED)

if __name__ == '__main__':
    base_infos = get_url('https://www.bilibili.com/video/BV1664y1r7bd?spm_id_from=333.337.search-card.all.click')
    print(base_infos)
    concurrent_download(base_infos)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mxs0523

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值