日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

使用Python腳本下載Bilibili相簿

系統(tǒng) 2136 0

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

下載Bilibili相簿

目錄

1. 接口展示
2. 代碼轉(zhuǎn)載
3. 代碼詳細(xì)注釋
4.總結(jié)

一、接口展示 ?

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號

實(shí)例:
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

二、代碼轉(zhuǎn)載 ?

            
              
                #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
              
                )
              
            
          

三、代碼詳細(xì)注釋 ?

            
              
                #encoding=utf-8
              
              
                import
              
               requests
              
                ,
              
              os
              
                ,
              
              sys 
              
                #導(dǎo)入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']取整后的結(jié)果
              
              
                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
              
                )
              
              
                #繼續(xù)對url進(jìn)行拼接,加上'&page_num'和str(page)的結(jié)果,最后重新賦值給url
              
              
                try
              
              
                :
              
              
        req
              
                =
              
              requests
              
                .
              
              get
              
                (
              
              url
              
                ,
              
              timeout
              
                =
              
              
                5
              
              
                )
              
              
                #嘗試請求拼接的url,超時設(shè)置為5秒,結(jié)果賦值給req
              
              
        rspJson 
              
                =
              
               req
              
                .
              
              json
              
                (
              
              
                )
              
              
                #json提取,賦值給rspJson
              
              
                # Get all items in a range
              
              
        items 
              
                =
              
               rspJson
              
                [
              
              
                'data'
              
              
                ]
              
              
                [
              
              
                'items'
              
              
                ]
              
              
                #提取rspJson['data']['items']的內(nèi)容,賦值給items
              
              
                for
              
               i 
              
                in
              
               items
              
                :
              
              
                #遍歷items
              
              
            urls 
              
                =
              
              
                {
              
              
                }
              
              
                #創(chuàng)建空字典,賦值給urls
              
              
            did 
              
                =
              
              
                str
              
              
                (
              
              i
              
                [
              
              
                'doc_id'
              
              
                ]
              
              
                )
              
              
                #提取i['doc_id']的內(nèi)容,并對它進(jìn)行字符串化操作,賦值給did
              
              
                # Single item traversal
              
              
            count 
              
                =
              
              
                0
              
              
                #給count賦值為0
              
              
                for
              
               j 
              
                in
              
               i
              
                [
              
              
                'pictures'
              
              
                ]
              
              
                :
              
              
                #遍歷i['pictures']
              
              
                urls
              
                [
              
              count
              
                ]
              
              
                =
              
              j
              
                [
              
              
                'img_src'
              
              
                ]
              
              
                #提取j['img_src']的內(nèi)容,鍵值對應(yīng)count和它的內(nèi)容
              
              
                count
              
                +=
              
              
                1
              
              
                #count自加1
              
              
                # Download
              
              
            downloadDraw
              
                (
              
              bid
              
                ,
              
              did
              
                ,
              
              urls
              
                )
              
              
                #將bid,did,urls參數(shù)傳入downloadDraw函數(shù)
              
              
                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循環(huán),循環(huán)次數(shù)為len(urls)的長度
              
              
        u 
              
                =
              
               urls
              
                [
              
              i
              
                ]
              
              
                #提取urls字典中的鍵i對應(yīng)的值,并把值賦給u
              
              
                try
              
              
                :
              
              
                # Get image format from url
              
              
            suffix 
              
                =
              
               u
              
                .
              
              split
              
                (
              
              
                "."
              
              
                )
              
              
                [
              
              
                -
              
              
                1
              
              
                ]
              
              
                #對u進(jìn)行split操作,取最后一個項(xiàng),賦值給suffix
              
              
                # File naming
              
              
                ## bid: Bilibili user id
              
              
                ## did: Draw id
              
              
            fileName 
              
                =
              
               did
              
                +
              
              
                '_b'
              
              
                +
              
              
                str
              
              
                (
              
              count
              
                )
              
              
                +
              
              
                '.'
              
              
                +
              
              suffix 
              
                #拼接did、'-b'、str(count)、'.'和suffix,結(jié)果賦值給fileName
              
              
                if
              
              
                (
              
              os
              
                .
              
              path
              
                .
              
              exists
              
                (
              
              
                './'
              
              
                +
              
              bid
              
                +
              
              
                '/'
              
              
                +
              
              fileName
              
                )
              
              
                )
              
              
                :
              
              
                #如果'./bid/fileName'存在,就跳過,count自加1,并結(jié)束本次循環(huán)
              
              
                print
              
              
                (
              
              
                'Skipped '
              
              
                +
              
              did
              
                +
              
              
                ' '
              
              
                +
              
              u
              
                )
              
              
                count
              
                +=
              
              
                1
              
              
                continue
              
              
                print
              
              
                (
              
              
                'Downloading '
              
              
                +
              
              did
              
                +
              
              
                ' '
              
              
                +
              
              u
              
                )
              
              
                #打印'Downloading'+did+' '+u的內(nèi)容
              
              
                # Download single image
              
              
            req 
              
                =
              
               requests
              
                .
              
              get
              
                (
              
              u
              
                ,
              
              timeout
              
                =
              
              
                20
              
              
                )
              
              
                #request請求,url為u,超時設(shè)置為20s,結(jié)果賦值給req 
              
              
                # Create image file
              
              
                with
              
              
                open
              
              
                (
              
              
                './'
              
              
                +
              
              bid
              
                +
              
              
                '/'
              
              
                +
              
              fileName
              
                ,
              
              
                'wb'
              
              
                )
              
              
                as
              
               f
              
                :
              
              
                #with上下文管理器,以'wb'格式打開'./'+bid+'/'+fileName,將對象命名為f
              
              
                f
              
                .
              
              write
              
                (
              
              req
              
                .
              
              content
              
                )
              
              
                #對f采用write方法,寫入req.content的內(nèi)容
              
              
                except
              
               Exception 
              
                as
              
               e
              
                :
              
              
                #異常處理
              
              
                print
              
              
                (
              
              e
              
                )
              
              
                #打印異常
              
              
                print
              
              
                (
              
              
                'Fail to download: '
              
              
                +
              
              did
              
                +
              
              
                ' '
              
              
                +
              
              u
              
                )
              
              
                #打印'Fail to download: '+did+' '+u
              
              
            
        count
              
                +=
              
              
                1
              
              
                #count自加1
              
              
                if
              
               __name__
              
                ==
              
              
                '__main__'
              
              
                :
              
              
                #當(dāng)做模塊導(dǎo)入時,__name__不等于__main__,將不運(yùn)行以下內(nèi)容
              
              
                #直接運(yùn)行時,__name__==__main__,運(yùn)行以下內(nèi)容
              
              
                if
              
              
                (
              
              
                len
              
              
                (
              
              sys
              
                .
              
              argv
              
                )
              
              
                <
              
              
                2
              
              
                )
              
              
                :
              
              
                #不給uid參數(shù),打印信息,并直接退出,這方式是在控制臺給uid參數(shù)使用
              
              
                print
              
              
                (
              
              
                'Please enter the bilibili user id.'
              
              
                )
              
              
        sys
              
                .
              
              exit
              
                (
              
              
                0
              
              
                )
              
              
    
    bid 
              
                =
              
              
                str
              
              
                (
              
              sys
              
                .
              
              argv
              
                [
              
              
                1
              
              
                ]
              
              
                )
              
              
                #提取所給的uid參數(shù)
              
              
                # Create drawer's directory #創(chuàng)建uid名的文件目錄
              
              
                try
              
              
                :
              
              
        os
              
                .
              
              makedirs
              
                (
              
              
                './'
              
              
                +
              
              bid
              
                )
              
              
                except
              
              
                :
              
              
                pass
              
              

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

四、總結(jié) ?

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

點(diǎn)我回頂部 ?

?
?
?
?
?
?
?
Fin.


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

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

【本文對您有幫助就好】

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

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 文昌市| 博白县| 新疆| 界首市| 绥棱县| 桃源县| 三台县| 许昌县| 西丰县| 通化市| 中宁县| 宝丰县| 长白| 泸州市| 西丰县| 章丘市| 阿瓦提县| 仪陇县| 景东| 霞浦县| 阳朔县| 望江县| 鹤庆县| 小金县| 景东| 平乡县| 海门市| 黔江区| 宁陕县| 贵州省| 兰考县| 宁国市| 桂东县| 汨罗市| 新郑市| 三江| 吉水县| 靖远县| 二连浩特市| 仁寿县| 饶河县|