Python 爬取 B 站 UP 主相册所有图片

参考网址:

https://www.52pojie.cn/thread-1202155-1-1.html

https://www.52pojie.cn/thread-1202189-1-1.html

源代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import json
import os
import requests

requests.packages.urllib3.disable_warnings() # 作用是去掉警告信息,仅仅是为了美观,可以去掉
uid = input("请输入B站UP主的UID:")
# User-Agent可自行获取与修改
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36'}
pic_list = [] # 存放图片链接
page = 0

while True:
url = 'https://api.vc.bilibili.com/link_draw/v1/doc/doc_list?uid=' + str(uid) + '&page_num=' + str(page) + '&page_size=30&biz=all'
page += 1
content = requests.get(url, headers=headers, verify=False).content
dic = json.loads(content) # 将json转化为字典
item_list = dic.get('data').get('items') # 获取项目信息列表

if len(item_list) == 0: # 如果爬完了就停止
break

for i in item_list:
pic = i.get('pictures')[0].get('img_src')
pic_list.append(pic)

os.mkdir('./' + str(uid))
count = 1

for pic_url in pic_list:
content = requests.get(pic_url, headers=headers, verify=False).content

with open('./' + str(uid) + '/' + str(count) + '.' + pic_url[-3:], 'wb') as f:
f.write(content)

count += 1
} ())

使用方法:

输入 UP 主的 UID 即可。UID 如下图所示:

会在当前目录新建一个以 UID 命名的文件夹,图片下载到文件夹中,且已经编号。


Python 爬取 B 站 UP 主相册所有图片
https://roachlin.github.io/2021-01-14-bili-img/
作者
RoachLin
发布于
2021年1月14日
许可协议