From 7cc0ea0ef3485914af2af74a3038a13659ebad97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=9F=A5=E5=BE=AE?= Date: Fri, 3 Jul 2026 13:34:31 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20Eastmoney=201-fail=E2=86=92Tencent=20+?= =?UTF-8?q?=202s=20interval=20+=20HKD=E2=86=92CNY=20+=20None-safe?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- price_monitor.py | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/price_monitor.py b/price_monitor.py index b6d24e5..f29f4d9 100644 --- a/price_monitor.py +++ b/price_monitor.py @@ -131,39 +131,32 @@ def fetch_hk_eastmoney(codes): results = {} - # 主通道:东方财富实时行情(逐股查询,2秒超时。连续失败则立即切腾讯兜底) - consecutive_fail = 0 + # 主通道:东方财富实时行情。单个请求5s超时,失败立刻切腾讯。 + # 实测限流阈值约30秒,间隔2秒可稳定运行(15只港股30秒内完成) for code in hk_codes: try: - if consecutive_fail >= 3: - break # 前3只都失败,东财不可用,直接切腾讯 url = (f"https://push2.eastmoney.com/api/qt/stock/get" f"?secid=116.{code}" f"&fields=f43,f170,f60,f57,f58" f"&fltt=2") req = urllib.request.Request(url, headers={"User-Agent": UA}) - with urllib.request.urlopen(req, timeout=2) as r: + with urllib.request.urlopen(req, timeout=5) as r: resp = json.loads(r.read().decode("utf-8")) if resp.get("rc") != 0: - consecutive_fail += 1 - continue + break # 东财不可用,切腾讯 item = resp.get("data", {}) if not item: - consecutive_fail += 1 - continue + break price = float(item.get("f43", 0)) if item.get("f43") else 0 prev_close = float(item.get("f60", 0)) if item.get("f60") else 0 change = round(price - prev_close, 2) if prev_close > 0 else 0 change_pct = str(item.get("f170", "0")) if price > 0: results[code] = (price, change, change_pct) - consecutive_fail = 0 # 成功后重置计数 - time.sleep(0.1) - except Exception as e: - consecutive_fail += 1 - if consecutive_fail <= 2: - print(f"⚠️ 东方财富 {code} 拉取失败: {e}", file=sys.stderr) + time.sleep(2) # 防止触发东财限流(实测阈值~30s) + except Exception: + break # 东财不可用,立刻切腾讯 # Fallback: 腾讯 qt.gtimg.cn(15分钟延迟) missing = [c for c in hk_codes if c not in results]