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