fix: Eastmoney individual query + remove /100 price bug

This commit is contained in:
hmo
2026-06-30 00:29:37 +08:00
parent 8710dfe366
commit d9b48ea5f0
+27 -45
View File
@@ -132,51 +132,33 @@ def fetch_hk_eastmoney(codes):
results = {}
# 主通道:东方财富实时行情
try:
secids = ",".join(f"116.{c}" for c in hk_codes)
url = (f"https://push2.eastmoney.com/api/qt/stock/get"
f"?secid={secids}"
f"&fields=f43,f44,f45,f46,f47,f48,f57,f58,f169,f170,f60"
f"&fltt=2&invt=2")
req = urllib.request.Request(url, headers={"User-Agent": UA})
with urllib.request.urlopen(req, timeout=8) as r:
data = json.loads(r.read().decode("utf-8"))
# 解析:可能是单条 {"data":{...}} 或多条 {"data":[{...},...]}
items = data.get("data")
if items is None:
pass
elif isinstance(items, dict):
items = [items]
if isinstance(items, list):
for item in items:
if not item:
continue
# 从 secid 反推原始代码: "116.00700" → "00700"
raw_secid = str(item.get("secid", item.get("code", "")))
code = raw_secid.replace("116.", "").replace("116.", "")
if not code:
continue
# 找原始codes中匹配的
matched = None
code_padded = code.zfill(5)
for c in hk_codes:
if c == code or c == code_padded or code_padded.endswith(c):
matched = c
break
if not matched:
continue
price = float(item.get("f43", 0)) / 100 if item.get("f43") else 0
prev_close = float(item.get("f60", 0)) / 100 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[matched] = (price, change, change_pct)
except Exception as e:
print(f"⚠️ 东方财富港股拉取失败: {e}", file=sys.stderr)
# 主通道:东方财富实时行情(逐股查询,港股最多~10只,可接受)
for code in hk_codes:
try:
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=5) as r:
resp = json.loads(r.read().decode("utf-8"))
if resp.get("rc") != 0:
continue
item = resp.get("data", {})
if not item:
continue
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)
# 东方财富有频率限制,每请求间隔 0.2s
time.sleep(0.2)
except Exception as e:
print(f"⚠️ 东方财富 {code} 拉取失败: {e}", file=sys.stderr)
continue
# Fallback: 腾讯 qt.gtimg.cn15分钟延迟)
missing = [c for c in hk_codes if c not in results]