fix: Eastmoney 1-fail→Tencent + 2s interval + HKD→CNY + None-safe

This commit is contained in:
知微
2026-07-03 13:34:31 +08:00
parent c1c4ba4a81
commit 7cc0ea0ef3
+8 -15
View File
@@ -131,39 +131,32 @@ def fetch_hk_eastmoney(codes):
results = {} results = {}
# 主通道:东方财富实时行情(逐股查询,2秒超时。连续失败则立即切腾讯兜底) # 主通道:东方财富实时行情。单个请求5s超时,失败立刻切腾讯。
consecutive_fail = 0 # 实测限流阈值约30秒,间隔2秒可稳定运行(15只港股30秒内完成)
for code in hk_codes: for code in hk_codes:
try: try:
if consecutive_fail >= 3:
break # 前3只都失败,东财不可用,直接切腾讯
url = (f"https://push2.eastmoney.com/api/qt/stock/get" url = (f"https://push2.eastmoney.com/api/qt/stock/get"
f"?secid=116.{code}" f"?secid=116.{code}"
f"&fields=f43,f170,f60,f57,f58" f"&fields=f43,f170,f60,f57,f58"
f"&fltt=2") f"&fltt=2")
req = urllib.request.Request(url, headers={"User-Agent": UA}) 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")) resp = json.loads(r.read().decode("utf-8"))
if resp.get("rc") != 0: if resp.get("rc") != 0:
consecutive_fail += 1 break # 东财不可用,切腾讯
continue
item = resp.get("data", {}) item = resp.get("data", {})
if not item: if not item:
consecutive_fail += 1 break
continue
price = float(item.get("f43", 0)) if item.get("f43") else 0 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 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 = round(price - prev_close, 2) if prev_close > 0 else 0
change_pct = str(item.get("f170", "0")) change_pct = str(item.get("f170", "0"))
if price > 0: if price > 0:
results[code] = (price, change, change_pct) results[code] = (price, change, change_pct)
consecutive_fail = 0 # 成功后重置计数 time.sleep(2) # 防止触发东财限流(实测阈值~30s)
time.sleep(0.1) except Exception:
except Exception as e: break # 东财不可用,立刻切腾讯
consecutive_fail += 1
if consecutive_fail <= 2:
print(f"⚠️ 东方财富 {code} 拉取失败: {e}", file=sys.stderr)
# Fallback: 腾讯 qt.gtimg.cn15分钟延迟) # Fallback: 腾讯 qt.gtimg.cn15分钟延迟)
missing = [c for c in hk_codes if c not in results] missing = [c for c in hk_codes if c not in results]