欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

使用Python腳本下載Bilibili相簿

系統 2070 0

參考鏈接:Bilibili相簿下載(Bilibili Album Download)

下載Bilibili相簿

目錄

1. 接口展示
2. 代碼轉載
3. 代碼詳細注釋
4.總結

一、接口展示 ?

api1: https://api.vc.bilibili.com/link_draw/v1/doc/upload_count?uid= +uid號
api2: https://api.vc.bilibili.com/link_draw/v1/doc/doc_list?page_size=30&biz=all&uid= +uid號

實例:
uid = 2080663
api1 = https://api.vc.bilibili.com/link_draw/v1/doc/upload_count?uid=2080663
api2 = https://api.vc.bilibili.com/link_draw/v1/doc/doc_list?page_size=30&biz=all&uid=2080663

二、代碼轉載 ?

            
              
                #encoding=utf-8
              
              
                import
              
               requests
              
                ,
              
              os
              
                ,
              
              sys

basicApiUrl
              
                =
              
              
                'https://api.vc.bilibili.com/link_draw/v1/doc/upload_count?uid='
              
              
apiUrl
              
                =
              
              
                'https://api.vc.bilibili.com/link_draw/v1/doc/doc_list?page_size=30&biz=all&uid='
              
              
                # Get the amount of all draws
              
              
                # If error return 0
              
              
                def
              
              
                getTotalDraw
              
              
                (
              
              bid
              
                )
              
              
                :
              
              
                try
              
              
                :
              
              
        req
              
                =
              
              requests
              
                .
              
              get
              
                (
              
              basicApiUrl
              
                +
              
              bid
              
                )
              
              
        rspJson 
              
                =
              
               req
              
                .
              
              json
              
                (
              
              
                )
              
              
                except
              
              
                :
              
              
                return
              
              
                0
              
              
                if
              
              
                (
              
              
                'data'
              
              
                in
              
               rspJson 
              
                and
              
              
                'all_count'
              
              
                in
              
               rspJson
              
                [
              
              
                'data'
              
              
                ]
              
              
                )
              
              
                :
              
              
                return
              
              
                int
              
              
                (
              
              rspJson
              
                [
              
              
                'data'
              
              
                ]
              
              
                [
              
              
                'all_count'
              
              
                ]
              
              
                )
              
              
                return
              
              
                0
              
              
                # Get the draw list, 30 draws in each page
              
              
                def
              
              
                downloadDrawList
              
              
                (
              
              bid
              
                ,
              
              page
              
                )
              
              
                :
              
              
    url 
              
                =
              
               apiUrl
              
                +
              
              bid
    
    
              
                # Add page num
              
              
    url 
              
                =
              
               url
              
                +
              
              
                '&page_num='
              
              
                +
              
              
                str
              
              
                (
              
              page
              
                )
              
              
                try
              
              
                :
              
              
        req
              
                =
              
              requests
              
                .
              
              get
              
                (
              
              url
              
                ,
              
              timeout
              
                =
              
              
                5
              
              
                )
              
              
        rspJson 
              
                =
              
               req
              
                .
              
              json
              
                (
              
              
                )
              
              
                # Get all items in a range
              
              
        items 
              
                =
              
               rspJson
              
                [
              
              
                'data'
              
              
                ]
              
              
                [
              
              
                'items'
              
              
                ]
              
              
                for
              
               i 
              
                in
              
               items
              
                :
              
              
            urls 
              
                =
              
              
                {
              
              
                }
              
              
            did 
              
                =
              
              
                str
              
              
                (
              
              i
              
                [
              
              
                'doc_id'
              
              
                ]
              
              
                )
              
              
                # Single item traversal
              
              
            count 
              
                =
              
              
                0
              
              
                for
              
               j 
              
                in
              
               i
              
                [
              
              
                'pictures'
              
              
                ]
              
              
                :
              
              
                urls
              
                [
              
              count
              
                ]
              
              
                =
              
              j
              
                [
              
              
                'img_src'
              
              
                ]
              
              
                count
              
                +=
              
              
                1
              
              
                # Download
              
              
            downloadDraw
              
                (
              
              bid
              
                ,
              
              did
              
                ,
              
              urls
              
                )
              
              
                except
              
               Exception 
              
                as
              
               e
              
                :
              
              
                print
              
              
                (
              
              e
              
                )
              
              
                pass
              
              
                # Download draws
              
              
                def
              
              
                downloadDraw
              
              
                (
              
              bid
              
                ,
              
              did
              
                ,
              
              urls
              
                )
              
              
                :
              
              
    count 
              
                =
              
              
                0
              
              
                for
              
               i 
              
                in
              
              
                range
              
              
                (
              
              
                len
              
              
                (
              
              urls
              
                )
              
              
                )
              
              
                :
              
              
        u 
              
                =
              
               urls
              
                [
              
              i
              
                ]
              
              
                try
              
              
                :
              
              
                # Get image format from url
              
              
            suffix 
              
                =
              
               u
              
                .
              
              split
              
                (
              
              
                "."
              
              
                )
              
              
                [
              
              
                -
              
              
                1
              
              
                ]
              
              
                # File naming
              
              
                ## bid: Bilibili user id
              
              
                ## did: Draw id
              
              
            fileName 
              
                =
              
               did
              
                +
              
              
                '_b'
              
              
                +
              
              
                str
              
              
                (
              
              count
              
                )
              
              
                +
              
              
                '.'
              
              
                +
              
              suffix
            
            
              
                if
              
              
                (
              
              os
              
                .
              
              path
              
                .
              
              exists
              
                (
              
              
                './'
              
              
                +
              
              bid
              
                +
              
              
                '/'
              
              
                +
              
              fileName
              
                )
              
              
                )
              
              
                :
              
              
                print
              
              
                (
              
              
                'Skipped '
              
              
                +
              
              did
              
                +
              
              
                ' '
              
              
                +
              
              u
              
                )
              
              
                count
              
                +=
              
              
                1
              
              
                continue
              
              
                print
              
              
                (
              
              
                'Downloading '
              
              
                +
              
              did
              
                +
              
              
                ' '
              
              
                +
              
              u
              
                )
              
              
                # Download single image
              
              
            req 
              
                =
              
               requests
              
                .
              
              get
              
                (
              
              u
              
                ,
              
              timeout
              
                =
              
              
                20
              
              
                )
              
              
                # Create image file
              
              
                with
              
              
                open
              
              
                (
              
              
                './'
              
              
                +
              
              bid
              
                +
              
              
                '/'
              
              
                +
              
              fileName
              
                ,
              
              
                'wb'
              
              
                )
              
              
                as
              
               f
              
                :
              
              
                f
              
                .
              
              write
              
                (
              
              req
              
                .
              
              content
              
                )
              
              
                except
              
               Exception 
              
                as
              
               e
              
                :
              
              
                print
              
              
                (
              
              e
              
                )
              
              
                print
              
              
                (
              
              
                'Fail to download: '
              
              
                +
              
              did
              
                +
              
              
                ' '
              
              
                +
              
              u
              
                )
              
              
            
        count
              
                +=
              
              
                1
              
              
                if
              
               __name__
              
                ==
              
              
                '__main__'
              
              
                :
              
              
                if
              
              
                (
              
              
                len
              
              
                (
              
              sys
              
                .
              
              argv
              
                )
              
              
                <
              
              
                2
              
              
                )
              
              
                :
              
              
                print
              
              
                (
              
              
                'Please enter the bilibili user id.'
              
              
                )
              
              
        sys
              
                .
              
              exit
              
                (
              
              
                0
              
              
                )
              
              
    
    bid 
              
                =
              
              
                str
              
              
                (
              
              sys
              
                .
              
              argv
              
                [
              
              
                1
              
              
                ]
              
              
                )
              
              
                # Create drawer's directory
              
              
                try
              
              
                :
              
              
        os
              
                .
              
              makedirs
              
                (
              
              
                './'
              
              
                +
              
              bid
              
                )
              
              
                except
              
              
                :
              
              
                pass
              
              

    totalDraw 
              
                =
              
               getTotalDraw
              
                (
              
              bid
              
                )
              
              
    totalPage 
              
                =
              
              
                int
              
              
                (
              
              totalDraw
              
                /
              
              
                30
              
              
                )
              
              
                +
              
              
                1
              
              
                if
              
               totalDraw 
              
                %
              
              
                30
              
              
                !=
              
              
                0
              
              
                else
              
               totalDraw
              
                /
              
              
                30
              
              
                for
              
               page 
              
                in
              
              
                range
              
              
                (
              
              totalPage
              
                )
              
              
                :
              
              
        downloadDrawList
              
                (
              
              bid
              
                ,
              
              page
              
                )
              
            
          

三、代碼詳細注釋 ?

            
              
                #encoding=utf-8
              
              
                import
              
               requests
              
                ,
              
              os
              
                ,
              
              sys 
              
                #導入requests,os,sys模塊
              
              

basicApiUrl
              
                =
              
              
                'https://api.vc.bilibili.com/link_draw/v1/doc/upload_count?uid='
              
              
                #賦值操作
              
              
apiUrl
              
                =
              
              
                'https://api.vc.bilibili.com/link_draw/v1/doc/doc_list?page_size=30&biz=all&uid='
              
              
                #同上
              
              
                # Get the amount of all draws
              
              
                # If error return 0
              
              
                def
              
              
                getTotalDraw
              
              
                (
              
              bid
              
                )
              
              
                :
              
              
                try
              
              
                :
              
              
        req
              
                =
              
              requests
              
                .
              
              get
              
                (
              
              basicApiUrl
              
                +
              
              bid
              
                )
              
              
                #request請求,url鏈接為basicApiUrl+bid拼接后的url
              
              
        rspJson 
              
                =
              
               req
              
                .
              
              json
              
                (
              
              
                )
              
              
                #json提取
              
              
                except
              
              
                :
              
              
                return
              
              
                0
              
              
                if
              
              
                (
              
              
                'data'
              
              
                in
              
               rspJson 
              
                and
              
              
                'all_count'
              
              
                in
              
               rspJson
              
                [
              
              
                'data'
              
              
                ]
              
              
                )
              
              
                :
              
              
                #如果rspJson中有'data'鍵
              
              
                return
              
              
                int
              
              
                (
              
              rspJson
              
                [
              
              
                'data'
              
              
                ]
              
              
                [
              
              
                'all_count'
              
              
                ]
              
              
                )
              
              
                #并且rspJson['data']中有'all_count'鍵,則返回rspJson['data']['all_count']取整后的結果
              
              
                return
              
              
                0
              
              
                #否則,返回0
              
              
                # Get the draw list, 30 draws in each page
              
              
                def
              
              
                downloadDrawList
              
              
                (
              
              bid
              
                ,
              
              page
              
                )
              
              
                :
              
              
    url 
              
                =
              
               apiUrl
              
                +
              
              bid 
              
                #拼接apiUrl和bid,賦值給url
              
              
                # Add page num
              
              
    url 
              
                =
              
               url
              
                +
              
              
                '&page_num='
              
              
                +
              
              
                str
              
              
                (
              
              page
              
                )
              
              
                #繼續對url進行拼接,加上'&page_num'和str(page)的結果,最后重新賦值給url
              
              
                try
              
              
                :
              
              
        req
              
                =
              
              requests
              
                .
              
              get
              
                (
              
              url
              
                ,
              
              timeout
              
                =
              
              
                5
              
              
                )
              
              
                #嘗試請求拼接的url,超時設置為5秒,結果賦值給req
              
              
        rspJson 
              
                =
              
               req
              
                .
              
              json
              
                (
              
              
                )
              
              
                #json提取,賦值給rspJson
              
              
                # Get all items in a range
              
              
        items 
              
                =
              
               rspJson
              
                [
              
              
                'data'
              
              
                ]
              
              
                [
              
              
                'items'
              
              
                ]
              
              
                #提取rspJson['data']['items']的內容,賦值給items
              
              
                for
              
               i 
              
                in
              
               items
              
                :
              
              
                #遍歷items
              
              
            urls 
              
                =
              
              
                {
              
              
                }
              
              
                #創建空字典,賦值給urls
              
              
            did 
              
                =
              
              
                str
              
              
                (
              
              i
              
                [
              
              
                'doc_id'
              
              
                ]
              
              
                )
              
              
                #提取i['doc_id']的內容,并對它進行字符串化操作,賦值給did
              
              
                # Single item traversal
              
              
            count 
              
                =
              
              
                0
              
              
                #給count賦值為0
              
              
                for
              
               j 
              
                in
              
               i
              
                [
              
              
                'pictures'
              
              
                ]
              
              
                :
              
              
                #遍歷i['pictures']
              
              
                urls
              
                [
              
              count
              
                ]
              
              
                =
              
              j
              
                [
              
              
                'img_src'
              
              
                ]
              
              
                #提取j['img_src']的內容,鍵值對應count和它的內容
              
              
                count
              
                +=
              
              
                1
              
              
                #count自加1
              
              
                # Download
              
              
            downloadDraw
              
                (
              
              bid
              
                ,
              
              did
              
                ,
              
              urls
              
                )
              
              
                #將bid,did,urls參數傳入downloadDraw函數
              
              
                except
              
               Exception 
              
                as
              
               e
              
                :
              
              
                #異常處理
              
              
                print
              
              
                (
              
              e
              
                )
              
              
                #打印異常
              
              
                pass
              
              
                #略過
              
              
                # Download draws
              
              
                def
              
              
                downloadDraw
              
              
                (
              
              bid
              
                ,
              
              did
              
                ,
              
              urls
              
                )
              
              
                :
              
              
    count 
              
                =
              
              
                0
              
              
                #給count賦值為0
              
              
                for
              
               i 
              
                in
              
              
                range
              
              
                (
              
              
                len
              
              
                (
              
              urls
              
                )
              
              
                )
              
              
                :
              
              
                #for循環,循環次數為len(urls)的長度
              
              
        u 
              
                =
              
               urls
              
                [
              
              i
              
                ]
              
              
                #提取urls字典中的鍵i對應的值,并把值賦給u
              
              
                try
              
              
                :
              
              
                # Get image format from url
              
              
            suffix 
              
                =
              
               u
              
                .
              
              split
              
                (
              
              
                "."
              
              
                )
              
              
                [
              
              
                -
              
              
                1
              
              
                ]
              
              
                #對u進行split操作,取最后一個項,賦值給suffix
              
              
                # File naming
              
              
                ## bid: Bilibili user id
              
              
                ## did: Draw id
              
              
            fileName 
              
                =
              
               did
              
                +
              
              
                '_b'
              
              
                +
              
              
                str
              
              
                (
              
              count
              
                )
              
              
                +
              
              
                '.'
              
              
                +
              
              suffix 
              
                #拼接did、'-b'、str(count)、'.'和suffix,結果賦值給fileName
              
              
                if
              
              
                (
              
              os
              
                .
              
              path
              
                .
              
              exists
              
                (
              
              
                './'
              
              
                +
              
              bid
              
                +
              
              
                '/'
              
              
                +
              
              fileName
              
                )
              
              
                )
              
              
                :
              
              
                #如果'./bid/fileName'存在,就跳過,count自加1,并結束本次循環
              
              
                print
              
              
                (
              
              
                'Skipped '
              
              
                +
              
              did
              
                +
              
              
                ' '
              
              
                +
              
              u
              
                )
              
              
                count
              
                +=
              
              
                1
              
              
                continue
              
              
                print
              
              
                (
              
              
                'Downloading '
              
              
                +
              
              did
              
                +
              
              
                ' '
              
              
                +
              
              u
              
                )
              
              
                #打印'Downloading'+did+' '+u的內容
              
              
                # Download single image
              
              
            req 
              
                =
              
               requests
              
                .
              
              get
              
                (
              
              u
              
                ,
              
              timeout
              
                =
              
              
                20
              
              
                )
              
              
                #request請求,url為u,超時設置為20s,結果賦值給req 
              
              
                # Create image file
              
              
                with
              
              
                open
              
              
                (
              
              
                './'
              
              
                +
              
              bid
              
                +
              
              
                '/'
              
              
                +
              
              fileName
              
                ,
              
              
                'wb'
              
              
                )
              
              
                as
              
               f
              
                :
              
              
                #with上下文管理器,以'wb'格式打開'./'+bid+'/'+fileName,將對象命名為f
              
              
                f
              
                .
              
              write
              
                (
              
              req
              
                .
              
              content
              
                )
              
              
                #對f采用write方法,寫入req.content的內容
              
              
                except
              
               Exception 
              
                as
              
               e
              
                :
              
              
                #異常處理
              
              
                print
              
              
                (
              
              e
              
                )
              
              
                #打印異常
              
              
                print
              
              
                (
              
              
                'Fail to download: '
              
              
                +
              
              did
              
                +
              
              
                ' '
              
              
                +
              
              u
              
                )
              
              
                #打印'Fail to download: '+did+' '+u
              
              
            
        count
              
                +=
              
              
                1
              
              
                #count自加1
              
              
                if
              
               __name__
              
                ==
              
              
                '__main__'
              
              
                :
              
              
                #當做模塊導入時,__name__不等于__main__,將不運行以下內容
              
              
                #直接運行時,__name__==__main__,運行以下內容
              
              
                if
              
              
                (
              
              
                len
              
              
                (
              
              sys
              
                .
              
              argv
              
                )
              
              
                <
              
              
                2
              
              
                )
              
              
                :
              
              
                #不給uid參數,打印信息,并直接退出,這方式是在控制臺給uid參數使用
              
              
                print
              
              
                (
              
              
                'Please enter the bilibili user id.'
              
              
                )
              
              
        sys
              
                .
              
              exit
              
                (
              
              
                0
              
              
                )
              
              
    
    bid 
              
                =
              
              
                str
              
              
                (
              
              sys
              
                .
              
              argv
              
                [
              
              
                1
              
              
                ]
              
              
                )
              
              
                #提取所給的uid參數
              
              
                # Create drawer's directory #創建uid名的文件目錄
              
              
                try
              
              
                :
              
              
        os
              
                .
              
              makedirs
              
                (
              
              
                './'
              
              
                +
              
              bid
              
                )
              
              
                except
              
              
                :
              
              
                pass
              
              

    totalDraw 
              
                =
              
               getTotalDraw
              
                (
              
              bid
              
                )
              
              
                #在getTotalDraw函數中傳入bid參數,得到的結果返還給totalDraw變量
              
              
    totalPage 
              
                =
              
              
                int
              
              
                (
              
              totalDraw
              
                /
              
              
                30
              
              
                )
              
              
                +
              
              
                1
              
              
                if
              
               totalDraw 
              
                %
              
              
                30
              
              
                !=
              
              
                0
              
              
                else
              
               totalDraw
              
                /
              
              
                30
              
              
                #如果totalDraw和30作取模運算,如果余數不等于0,將totalDraw除以30的結果取整后加1,否則直接除以30,最后將結果賦值給totalPage
              
              
                for
              
               page 
              
                in
              
              
                range
              
              
                (
              
              totalPage
              
                )
              
              
                :
              
              
                #進行for循環,數量為totalPage次
              
              
        downloadDrawList
              
                (
              
              bid
              
                ,
              
              page
              
                )
              
              
                #將bid和page參數,傳入downloadDrawList函數
              
            
          

四、總結 ?

其實沒有什么實質性的東西,換一種方式寫文章也挺有意思的。

點我回頂部 ?

?
?
?
?
?
?
?
Fin.


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。?!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 国产精品中文在线 | 久久精品.com | 国产亚洲综合成人91精品 | 2022国产成人福利精品视频 | 日韩不卡在线 | 欧美日韩综合视频 | 极品xxxx欧美一区二区 | 欧美性生活视频 | 亚洲一区在线免费 | 免费视频一区二区 | 国产在线播放免费 | 国产网站在线播放 | 蜜桃视频在线免费播放 | 国产精品一区二区三区久久久 | 国产综合视频在线观看 | 免费在线看a| 嫩草影院在线入口 | 亚洲色图在线视频 | 五月婷婷六月爱 | 国产精选经典三级小泽玛利亚 | 欧美综合成人网 | 一区二区三区四区免费 | 成人av一区二区三区 | 97在线碰碰观看免费高清 | 天天爱夜夜做 | 亚洲精品一区在线观看 | 天天影院免费看电影 | 国产在线看片 | 欧美日皮视频 | 亚洲欧美国产日本 | 国产羞羞视频免费在线观看 | 午夜电影网址 | 亚洲精品电影 | 亚洲精品中文字幕在线观看 | 免费网址在线观看 | 欧美精品九九99久久在观看 | 99热精品在线观看 | 久久久中文字幕日本 | 清草在线视频精品 | 国产精品久久久久久久四虎电影 | 欧美男女交配 |