diff --git a/data_platform/fallback.py b/data_platform/fallback.py index d9758390..b8df5a14 100644 --- a/data_platform/fallback.py +++ b/data_platform/fallback.py @@ -72,24 +72,31 @@ class FallbackManager: prefix = "sz" tq_symbol = f"{prefix}{code}" - # 计算天数 days = (datetime.strptime(end_date, "%Y-%m-%d") - datetime.strptime(start_date, "%Y-%m-%d")).days + 10 - url = f"https://web.ifzq.gtimg.cn/appstock/app/fqkline/get?param={tq_symbol},day,,{days}," + url = f"https://web.ifzq.gtimg.cn/appstock/app/fqkline/get?param={tq_symbol},day,{start_date},,{days}," try: import urllib.request, json as _json opener = urllib.request.build_opener(urllib.request.ProxyHandler({})) req = urllib.request.Request(url, headers={"User-Agent": "Mozilla/5.0"}) with opener.open(req, timeout=10) as r: - data = _json.loads(r.read()) - klines = data.get("data", {}).get(tq_symbol, {}).get("day", []) + resp = _json.loads(r.read()) + d = resp.get("data") + if not isinstance(d, dict): + return None + klines = d.get(tq_symbol, {}).get("day", []) if not klines: return None - df = pd.DataFrame(klines, columns=["date", "open", "close", "high", "low", "volume"]) - for c in ["open", "close", "high", "low", "volume"]: + df = pd.DataFrame(klines) + ncols = len(df.columns) + if ncols >= 7: + df.columns = ["date", "open", "close", "high", "low", "volume", "amount"][:ncols] + else: + df.columns = ["date", "open", "close", "high", "low", "volume"][:ncols] + if "amount" not in df.columns: + df["amount"] = 0.0 + for c in ["open", "close", "high", "low", "volume", "amount"]: df[c] = pd.to_numeric(df[c], errors="coerce").fillna(0) - df["amount"] = 0.0 df["date"] = pd.to_datetime(df["date"]).dt.strftime("%Y-%m-%d") - # Filter date range mask = (df["date"] >= start_date) & (df["date"] <= end_date) return df.loc[mask, ["date", "open", "high", "low", "close", "volume", "amount"]] except Exception as e: