python爬取b站视频

python爬取b站视频

解析下载地址

  • python爬取b站视频
  • 须知
  • 视频下载地址
        • 怎么提取?:
          • 方法1.
          • 方法2.
    • 视频下一集
  • 番剧下载地址

须知

b站视频和音频是分开的,想要合并就要安装ffmpeg,在命令行中输入(‘ffmpeg -i video.m4s -i audio.m4s -c copy video.mp4’)
想要下载大会员的清晰度,请开大会员,再在header中加入cookie(下大会员番剧同理)

视频下载地址

打开一个视频url地址b站ig vs edg.https://www.bilibili.com/video/BV1zN41197N9
按F12(功能键)查看元素,
python爬取b站视频_第1张图片
这段代码中有下载地址,放到在线格式化地址中

window.__playinfo__ = {
     
    "code": 0,
    "message": "0",
    "ttl": 1,
    "data": {
     
        "from": "local",
        "result": "suee",
        "message": "",
        "quality": 80,
        "format": "flv",
        "timelength": 3135215,
        "accept_format": "flv_p60,flv,flv720_p60,flv480,mp4",
        "accept_description": ["高清 1080P60", "高清 1080P", "高清 720P60", "清晰 480P", "流畅 360P"],
        "accept_quality": [116, 80, 64, 32, 16],
        "video_codecid": 7,
        "seek_param": "start",
        "seek_type": "offset",
        "dash": {
     
            "duration": 3136,
            "minBufferTime": 1.5,
            "min_buffer_time": 1.5,
            "video": [{
     
                "id": 80,
                "baseUrl": "https://upos-sz-mirrorkodo.bilivideo.com/upgcxcode/04/89/303338904/303338904_da3-1-30080.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1616230265&gen=playurlv2&os=kodobv&oi=1891726869&trid=37e8b45dbdc64feeb574c238040a4c1au&platform=pc&upsig=aa826e91ee90c9632f65c23d63caa7bf&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&mid=519211528&orderid=0,3&agrr=1&logo=80000000",
                "base_url": "https://upos-sz-mirrorkodo.bilivideo.com/upgcxcode/04/89/303338904/303338904_da3-1-30080.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1616230265&gen=playurlv2&os=kodobv&oi=1891726869&trid=37e8b45dbdc64feeb574c238040a4c1au&platform=pc&upsig=aa826e91ee90c9632f65c23d63caa7bf&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&mid=519211528&orderid=0,3&agrr=1&logo=80000000",
                "backupUrl": ["https://upos-sz-mirrorkodo.bilivideo.com/upgcxcode/04/89/303338904/303338904_da3-1-30080.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1616230265&gen=playurlv2&os=kodobv&oi=1891726869&trid=37e8b45dbdc64feeb574c238040a4c1au&platform=pc&upsig=aa826e91ee90c9632f65c23d63caa7bf&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&mid=519211528&orderid=1,3&agrr=1&logo=40000000", "https://upos-sz-mirrorkodob.bilivideo.com/upgcxcode/04/89/303338904/303338904_da3-1-30080.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1616230265&gen=playurlv2&os=kodobbv&oi=1891726869&trid=37e8b45dbdc64feeb574c238040a4c1au&platform=pc&upsig=f4e91e09abb7a45d8a21b90ac604cfd3&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&mid=519211528&orderid=2,3&agrr=1&logo=40000000"],
                "backup_url": ["https://upos-sz-mirrorkodo.bilivideo.com/upgcxcode/04/89/303338904/303338904_da3-1-30080.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1616230265&gen=playurlv2&os=kodobv&oi=1891726869&trid=37e8b45dbdc64feeb574c238040a4c1au&platform=pc&upsig=aa826e91ee90c9632f65c23d63caa7bf&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&mid=519211528&orderid=1,3&agrr=1&logo=40000000", "https://upos-sz-mirrorkodob.bilivideo.com/upgcxcode/04/89/303338904/303338904_da3-1-30080.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1616230265&gen=playurlv2&os=kodobbv&oi=1891726869&trid=37e8b45dbdc64feeb574c238040a4c1au&platform=pc&upsig=f4e91e09abb7a45d8a21b90ac604cfd3&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&mid=519211528&orderid=2,3&agrr=1&logo=40000000"],
                "bandwidth": 2628150,
                "mimeType": "video/mp4",
                "mime_type": "video/mp4",
                "codecs": "avc1.640032",
                "width": 1920,
                "height": 1080,
                "frameRate": "16000/544",
                "frame_rate": "16000/544",
                "sar": "1:1",
                "startWithSap": 1,
                "start_with_sap": 1,
                "SegmentBase": {
     
                    "Initialization": "0-1008",
                    "indexRange": "1009-8564"
                },
                "segment_base": {
     
                    "initialization": "0-1008",
                    "index_range": "1009-8564"
                },
                "codecid": 7
            },
            {
     
                "id": 80,
                "baseUrl": "https://upos-sz-mirrorcoso1.bilivideo.com/upgcxcode/04/89/303338904/303338904_da3-1-30077.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1616230265&gen=playurlv2&os=coso1bv&oi=1891726869&trid=37e8b45dbdc64feeb574c238040a4c1au&platform=pc&upsig=c9ccc6e99e6b047039c3eb00ab3f1673&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&mid=519211528&orderid=0,3&agrr=1&logo=80000000",
                "base_url": "https://upos-sz-mirrorcoso1.bilivideo.com/upgcxcode/04/89/303338904/303338904_da3-1-30077.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1616230265&gen=playurlv2&os=coso1bv&oi=1891726869&trid=37e8b45dbdc64feeb574c238040a4c1au&platform=pc&upsig=c9ccc6e99e6b047039c3eb00ab3f1673&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&mid=519211528&orderid=0,3&agrr=1&logo=80000000",
                "backupUrl": ["https://upos-sz-mirrorcoso1.bilivideo.com/upgcxcode/04/89/303338904/303338904_da3-1-30077.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1616230265&gen=playurlv2&os=coso1bv&oi=1891726869&trid=37e8b45dbdc64feeb574c238040a4c1au&platform=pc&upsig=c9ccc6e99e6b047039c3eb00ab3f1673&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&mid=519211528&orderid=1,3&agrr=1&logo=40000000", "https://upos-sz-mirrorcoso1.bilivideo.com/upgcxcode/04/89/303338904/303338904_da3-1-30077.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1616230265&gen=playurlv2&os=coso1bv&oi=1891726869&trid=37e8b45dbdc64feeb574c238040a4c1au&platform=pc&upsig=c9ccc6e99e6b047039c3eb00ab3f1673&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&mid=519211528&orderid=2,3&agrr=1&logo=40000000"],
                "backup_url": ["https://upos-sz-mirrorcoso1.bilivideo.com/upgcxcode/04/89/303338904/303338904_da3-1-30077.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1616230265&gen=playurlv2&os=coso1bv&oi=1891726869&trid=37e8b45dbdc64feeb574c238040a4c1au&platform=pc&upsig=c9ccc6e99e6b047039c3eb00ab3f1673&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&mid=519211528&orderid=1,3&agrr=1&logo=40000000", "https://upos-sz-mirrorcoso1.bilivideo.com/upgcxcode/04/89/303338904/303338904_da3-1-30077.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1616230265&gen=playurlv2&os=coso1bv&oi=1891726869&trid=37e8b45dbdc64feeb574c238040a4c1au&platform=pc&upsig=c9ccc6e99e6b047039c3eb00ab3f1673&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&mid=519211528&orderid=2,3&agrr=1&logo=40000000"],
                "bandwidth": 1577034,
                "mimeType": "video/mp4",
                "mime_type": "video/mp4",
                "codecs": "hev1.1.6.L120.90",
                "width": 1920,
                "height": 1080,
                "frameRate": "16000/544",
                "frame_rate": "16000/544",
                "sar": "1:1",
                "startWithSap": 1,
                "start_with_sap": 1,
                "SegmentBase": {
     
                    "Initialization": "0-1070",
                    "indexRange": "1071-8614"
                },
                "segment_base": {
     
                    "initialization": "0-1070",
                    "index_range": "1071-8614"
                },
                "codecid": 12
            },
            {
     
                "id": 64,
                "baseUrl": "https://upos-sz-mirrorcoso1.bilivideo.com/upgcxcode/04/89/303338904/303338904_da3-1-30074.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1616230265&gen=playurlv2&os=coso1bv&oi=1891726869&trid=37e8b45dbdc64feeb574c238040a4c1au&platform=pc&upsig=b3bb7eab794166b31372cec986ed4b82&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&mid=519211528&orderid=0,3&agrr=1&logo=80000000",
                "base_url": "https://upos-sz-mirrorcoso1.bilivideo.com/upgcxcode/04/89/303338904/303338904_da3-1-30074.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1616230265&gen=playurlv2&os=coso1bv&oi=1891726869&trid=37e8b45dbdc64feeb574c238040a4c1au&platform=pc&upsig=b3bb7eab794166b31372cec986ed4b82&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&mid=519211528&orderid=0,3&agrr=1&logo=80000000",
                "backupUrl": ["https://upos-sz-mirrorcoso1.bilivideo.com/upgcxcode/04/89/303338904/303338904_da3-1-30074.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1616230265&gen=playurlv2&os=coso1bv&oi=1891726869&trid=37e8b45dbdc64feeb574c238040a4c1au&platform=pc&upsig=b3bb7eab794166b31372cec986ed4b82&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&mid=519211528&orderid=1,3&agrr=1&logo=40000000", "https://upos-sz-mirrorcoso1.bilivideo.com/upgcxcode/04/89/303338904/303338904_da3-1-30074.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1616230265&gen=playurlv2&os=coso1bv&oi=1891726869&trid=37e8b45dbdc64f…"

太长了,就不放出所有了,video中的backUrl是视频下载地址(记得加ua和referer)对应的id编号
116:1080,60
80:1080
64:720
。。。一一对应的

怎么提取?:

方法1.

万能的正则表达式(只要条件够多,没有匹配不出来的)

方法2.

把前缀去掉,用json.loads()转化为json字典,再用字典的key,value提取

视频下一集

我在F12界面中没看到,但在页面源代码中看到了
python爬取b站视频_第2张图片
直接用python提取
python爬取b站视频_第3张图片

window.__INITIAL_STATE__ = {
     
    "aid": 501969861,
    "bvid": "BV1zN41197N9",
    "p": 1,
    "episode": "",
    "videoData": {
     
        "bvid": "BV1zN41197N9",
        "aid": 501969861,
        "videos": 3,
        "tid": 171,
        "tname": "电子竞技",
        "copyright": 1,
        "pic": "http:\u002F\u002Fi1.hdslb.com\u002Fbfs\u002Farchive\u002F84f0b8baa75672a82b76eb9f4ba7ff54b9d2c204.jpg",
        "title": "【LPL春季赛】2月26日 IG vs EDG",
        "pubdate": 1614344732,
        "ctime": 1614344732,
        "desc": "【LPL春季赛】2月26日 IG vs EDG",
        "state": 0,
        "duration": 6776,
        "rights": {
     
            "bp": 0,
            "elec": 0,
            "download": 1,
            "movie": 0,
            "pay": 0,
            "hd5": 0,
            "no_reprint": 1,
            "autoplay": 1,
            "ugc_pay": 0,
            "is_cooperation": 0,
            "ugc_pay_preview": 0,
            "no_background": 0,
            "clean_mode": 0,
            "is_stein_gate": 0
        },
        "owner": {
     
            "mid": 50329118,
            "name": "哔哩哔哩英雄联盟赛事",
            "face": "http:\u002F\u002Fi1.hdslb.com\u002Fbfs\u002Fface\u002Fcb620bbb9071974f37843134875d472b47532a97.jpg"
        },
        "stat": {
     
            "aid": 501969861,
            "view": 2045195,
            "danmaku": 28750,
            "reply": 10275,
            "favorite": 5748,
            "coin": 14128,
            "share": 1645,
            "now_rank": 0,
            "his_rank": 70,
            "like": 43130,
            "dislike": 0,
            "evaluation": "",
            "argue_msg": "",
            "viewseo": 2045195
        },
        "dynamic": "#电子竞技# #英雄联盟# #LPL春季赛# #EDG# #IG#【LPL春季赛】2月26日 IG vs EDG",
        "cid": 303338904,
        "dimension": {
     
            "width": 1920,
            "height": 1080,
            "rotate": 0
        },
        "no_cache": false,
        "pages": [{
     
            "cid": 303338904,
            "page": 1,
            "from": "vupload",
            "part": "第一局",
            "duration": 3136,
            "vid": "",
            "weblink": "",
            "dimension": {
     
                "width": 1920,
                "height": 1080,
                "rotate": 0
            }
        },
        {
     
            "cid": 303371329,
            "page": 2,
            "from": "vupload",
            "part": "第二局",
            "duration": 3426,
            "vid": "",
            "weblink": "",
            "dimension": {
     
                "width": 1920,
                "height": 1080,
                "rotate": 0
            }
        },
        {
     
            "cid": 303380964,
            "page": 3,
            "from": "vupload",
            "part": "赛后采访",
            "duration": 214,
            "vid": "",
            "weblink": "",
            "dimension": {
     
                "width": 1920,
                "height": 1080,
                "rotate": 0
            }
        }],
        "subtitle": {
     
            "allow_submit": false,
            "list": []
        },
        "user_garb": {
     
            "url_image_ani_cut": ""
        },
       

查看下一个视频就知道使用p来切换的
把page获取,name获取就行了

番剧下载地址

下载地址提取方式差不多,拿罪恶王冠.举例,用代码提取
python爬取b站视频_第4张图片
window.__playinfo是下载地址,
window__initial是下一集地址,提取方式差不多,但要记住要提取哪类链接(最好是ep的)
其他项用if过滤就行了。
这些就是下载b站视频的大致操作了。比iqiyi那些简单多了
最后一句,欢迎关注我的公众号,有什么问题可以直接问我,我如果知道一定会回答的

python爬取b站视频_第5张图片

你可能感兴趣的:(python,b站,爬虫,ffmpeg,python)