參考鏈接: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元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
