PythonAPI壁纸抓取

代码实现:

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
37
import requests
import os

# 创建保存目录
os.makedirs('123', exist_ok=True)

headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
'Referer': 'https://weibo.com/'
}

for i in range(1, 50):
try:
# 直接获取图片二进制流
response = requests.get(
url='https://api.iw233.cn/api.php?sort=iw233&num=2',
headers=headers,
stream=True,
timeout=15
)

# 获取文件扩展名
content_type = response.headers.get('Content-Type', 'image/jpeg')
ext = content_type.split('/')[-1]

# 生成文件名
file_name = f'123/pic_{i}.{ext}'

# 保存文件
with open(file_name, 'wb') as f:
for chunk in response.iter_content(1024):
f.write(chunk)
print(f"图片保存成功:{file_name}")

except Exception as e:
print(f"第{i}次请求失败:{str(e)}")

代码效果:

test

test2


PythonAPI壁纸抓取
http://example.com/2025/05/13/pythonAPI壁纸抓取/
Author
fwlw
Posted on
May 13, 2025
Licensed under