当前位置: 首页 > 工具软件 > bilibili-user > 使用案例 >

爬取bilibili视频---爬虫

司寇阳曦
2023-12-01

注意:仅用于测试,禁止非法使用

python代码
在所在文件夹创建文件夹download
暂时只支持bv号
下载文件是flv格式的,如需转成mp4可以用ffmpeg
要安装requests模块
pip安装

pip install requests

import json
import requests
import time
import os

def get_url_bv(bv):
    headerss={"User-Agent":"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.79 Safari/537.36 Maxthon/5.0"}
    def download_url(url,name,jia):
        path='download/'+jia+'/'#保存地址及名称
        r=requests.get(url,headers=headerss,stream=True)
        types=".flv"
        with open(path+name+types,'wb') as f:
            f.write(r.content)
            f.close()
        print(name+'文件保存成功')
    def mktdir(title):
        if os.path.exists("download/"+title)==True:
            print("文件夹已存在!")
        else:
            os.makedirs('download/'+title)
    r=requests.get("https://api.bilibili.com/x/web-interface/view?bvid="+bv,headers=headerss)
    j=json.loads(r.text)
    if(j["code"]==0):
        title=j["data"]["title"]
        mktdir(title)
        ciddick=j["data"]["pages"]
        lens=len(ciddick)
        while lens!=0:
            headerss={"User-Agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36"}
            cid=ciddick[lens-1]["cid"]
            name=ciddick[lens-1]["part"] 
            r=requests.get("https://api.bilibili.com/x/player/playurl?cid="+str(cid)+"&bvid="+bv+"&otype=json",headers=headerss)
            j=json.loads(r.text)
            linshi=j["data"]["durl"]
            url=linshi[0]["url"]
            headerss={"User-Agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36","Referer":"https://www.bilibili.com/video/"+bv}
            download_url(url,name,title)
            lens=lens-1
        return 0
    else:
        print("error:",j["code"]," message:",j["message"])
        return j["code"]

size=input("输入bv号个数:")
size=int(size)
for i in range(0,size):
    value=input("bv"+str(i)+":")
    get_url_bv(value)
 类似资料: