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)}")
|