我正在尝试解析用于管理我投资的股票(澳大利亚 ASX)的投资数据库的一些 JSON 数据。我是通过直接从 ASX 导出来做到这一点的,但他们最近采取了防止数据抓取的保护措施,因为需要利用另一个来源
我找到的最佳来源如下。下面的网址是
https://query1.finance.yahoo.com/v8/finance/chart/ETPMPM.AX?interval=1d&range=1d (其中 ETPMPM 是股票代码。其他还有 NDQ、PME、WOW 等)
这提供了 JSON 格式的数据,但我觉得它并不完全干净(我这样说是因为在线 Json 解析器指示错误)。
我想要的是得到一些不同的变量,但关键的是
图表->结果->元->常规市场价格 图表->结果->指标->报价->开仓 图表->结果->指标->报价->收盘 图表->结果->指标->报价->成交量
过去使用 json 我可以通过按键找到我想要的东西,但由于某种原因我不能这样做。
这是我当前代码相关部分的简化版本,它将 json 打印到屏幕上,但我无法让它提取任何特定变量
import requests
import json
def fetch_yahoo_finance_data():
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36'
}
url ="https://query1.finance.yahoo.com/v8/finance/chart/GGUS.AX?interval=1d&range=1d"
response = requests.get(url, headers=headers)
if response.status_code == 200:
data = response.json()
return data
else:
print("Failed to fetch data. Status Code{response.status_code}")
return None
data = fetch_yahoo_finance_data()
print(data)
#print((data['chart'][0]['result']['meta'])['currency']) #This line errors.Tried to base it off a similar nested json example online
#Following splits Json onto 1 field per line (normally) but in this case does nothing
for key, value in data.items():
print(key, value)
这提供了 JSON 格式的数据,但我觉得它并不完全干净。
我认为它应该是“干净的”,尝试这样的事情:
from typing import Any
import requests
HEADERS = {
'User-Agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
}
def fetch_stock_data(ticker: str) -> dict[str, Any]:
"""Fetches stock data for a given ticker from Yahoo Finance and extracts key variables.
Args:
ticker: The stock ticker symbol.
Returns:
A dictionary containing the extracted `regularMarketPrice`, `open`, `close`,
and `volume` values.
Raises:
ValueError: If the data cannot be parsed correctly or the expected fields are
missing.
"""
url = f'https://query1.finance.yahoo.com/v8/finance/chart/{ticker}?interval=1d&range=1d'
response = requests.get(url, headers=HEADERS)
data = response.json()
try:
result = data['chart']['result'][0]
meta = result['meta']
indicators = result['indicators']['quote'][0]
return {
'regularMarketPrice': meta.get('regularMarketPrice'),
'open': indicators.get('open', [None])[0],
'close': indicators.get('close', [None])[0],
'volume': indicators.get('volume', [None])[0]
}
except (KeyError, IndexError, TypeError) as e:
raise ValueError(
f'Error parsing data for ticker {ticker}: {str(e)}') from e
def main() -> None:
print(f'{fetch_stock_data("ETPMPM.AX") = }')
print(f'{fetch_stock_data("TSM") = }')
if __name__ == '__main__':
main()
2024-02-10输出:
fetch_stock_data("ETPMPM.AX") = {'regularMarketPrice': 0.0, 'open': 196.5, 'close': 194.5399932861328, 'volume': 176}
fetch_stock_data("TSM") = {'regularMarketPrice': 133.11, 'open': 134.5, 'close': 133.11000061035156, 'volume': 16976800}