fix: Eastmoney 1-fail→Tencent + 2s interval + HKD→CNY + None-safe
This commit is contained in:
+8
-15
@@ -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]
|
||||
|
||||
Reference in New Issue
Block a user