#!/usr/bin/env python3 """下载 254 套样板的二维平面图(pic_url,公开 OSS)。限速 0.2s。""" import json, os, time, urllib.request, urllib.error templates = json.load(open(r"D:\Code\huamenchuang\data\api-samples\windoor-templates-clean.json", encoding="utf-8")) OUT = r"D:\Code\huamenchuang\data\windoor-images" os.makedirs(OUT, exist_ok=True) ok, skip, fail = 0, 0, [] for i, t in enumerate(templates): ext = ".png" url = t["pic_url"] if url.lower().endswith((".jpg", ".jpeg")): ext = ".jpg" out = os.path.join(OUT, f"{t['id']}{ext}") if os.path.exists(out) and os.path.getsize(out) > 100: skip += 1 continue req = urllib.request.Request(url, headers={"User-Agent": "Mozilla/5.0"}) try: with urllib.request.urlopen(req, timeout=20) as r: data = r.read() open(out, "wb").write(data) ok += 1 except Exception as e: fail.append((t["id"], str(e)[:60])) if (i + 1) % 50 == 0: print(f"进度 {i+1}/{len(templates)} 成功{ok} 跳过{skip}", flush=True) time.sleep(0.2) print(f"完成: 新下载 {ok}, 已存在 {skip}, 失败 {len(fail)}") if fail: print(fail[:10])