up
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
"""新浪财经 API 客户端(K线降级数据源)"""
|
||||
|
||||
import httpx
|
||||
import json
|
||||
from typing import List
|
||||
|
||||
|
||||
async def fetch_history(code: str, days: int = 90) -> List[dict]:
|
||||
"""获取新浪财经日K线数据"""
|
||||
market = "sh" if code.startswith(("688", "60")) else "bj" if code.startswith(("920", "8", "4")) else "sz"
|
||||
symbol = f"{market}{code}"
|
||||
url = f"https://money.finance.sina.com.cn/quotes_service/api/json_v2.php/CN_MarketData.getKLineData?symbol={symbol}&scale=240&ma=no&datalen={days}"
|
||||
headers = {
|
||||
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36",
|
||||
}
|
||||
|
||||
async with httpx.AsyncClient() as client:
|
||||
try:
|
||||
resp = await client.get(url, headers=headers, timeout=10)
|
||||
if resp.status_code != 200:
|
||||
return []
|
||||
text = resp.text
|
||||
if not text or text.strip() in ("", "null"):
|
||||
return []
|
||||
arr = json.loads(text)
|
||||
if not isinstance(arr, list):
|
||||
return []
|
||||
result = []
|
||||
for item in arr:
|
||||
result.append({
|
||||
"date": item.get("day", ""),
|
||||
"open": float(item.get("open", 0) or 0),
|
||||
"close": float(item.get("close", 0) or 0),
|
||||
"high": float(item.get("high", 0) or 0),
|
||||
"low": float(item.get("low", 0) or 0),
|
||||
"volume": int(item.get("volume", 0) or 0),
|
||||
})
|
||||
return result
|
||||
except Exception:
|
||||
return []
|
||||
Reference in New Issue
Block a user