访问股票市场信息的Python API

2024-04-24 11:15:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我想知道是否有一个地方可以下载给定股票的元数据。我曾经研究过REST API,我想我可能会使用这样的东西:

stock_code = "GME" 
base_url = "https://somestockmarkekpage.com/api/stock?code={}" 
resp = requests.get(base_url.format(stock_code))
print(resp.json()['short_ratio'])

问题是我不知道从哪里可以下载这些数据,甚至不知道它是否免费存在。然而,您可以提供的任何其他API或服务都是非常受欢迎的


Tags: 数据httpscomrestapiurlbase地方
2条回答

您可以使用免费的Yahoo Finance API和他们最流行的Python库yfinance。 链接:https://pypi.org/project/yfinance/

示例代码:

import yfinance as yf

GME_data = yf.Ticker("GME")

# get stock info
GME_data.info

除此之外,您还可以使用许多其他API。您可以在RapidAPI中搜索并搜索“股票”

雅虎提供了一个免费的API,其中包含与多张票证相关的最新数据。您可以查看API详细信息here。从票据中提取元数据的一个示例是:

import yfinance as yf
stock_obj = yf.Ticker("GME")
# Here are some fixs on the JSON it returns
validated = str(stock_obj.info).replace("'","\"").replace("None", "\"NULL\"").replace("False", "\"FALSE\"").replace("True", "\"TRUE\"")
# Parsing the JSON here
meta_obj = json.loads(validated)

# Some of the short fields
print("sharesShort: "+str( meta_obj['sharesShort']))
print("shortRatio: "+str( meta_obj['shortRatio']))
print("shortPercentOfFloat: "+str( meta_obj['shortPercentOfFloat']))

您感兴趣的票据的输出为:

sharesShort: 61782730
shortRatio: 2.81 
shortPercentOfFloat: 2.2642

相关问题 更多 >