Files
quyun/frontend/wechat/avatar.py
2025-05-07 17:24:59 +08:00

29 lines
1020 B
Python

import requests
import os
# 创建保存目录
os.makedirs('avatar', 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'
}
for id in range(1, 80): # 生成1-79的ID
url = f'https://himg.bdimg.com/sys/portrait/hotitem/wildkid/{id}'
try:
response = requests.get(url, headers=headers, timeout=10)
if response.status_code == 200:
# 检查内容类型是否为图片
if 'image' in response.headers.get('Content-Type', ''):
with open(f'avatar/{id}.jpeg', 'wb') as f:
f.write(response.content)
print(f'ID {id} 下载成功')
else:
print(f'ID {id} 返回非图片内容,已跳过')
else:
print(f'ID {id} 响应码 {response.status_code},已跳过')
except Exception as e:
print(f'ID {id} 请求失败: {str(e)}')