【问题标题】:Python API to access Stock Market information用于访问股票市场信息的 Python API
【发布时间】:2021-01-31 18:27:36
【问题描述】:

我想知道是否有地方可以下载给定股票的元数据。我以前在研究 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'])

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

【问题讨论】:

    标签: python api rest


    【解决方案1】:

    Yahoo 提供了一个免费的 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
    

    【讨论】:

    • 谢谢!该 API 中是否有与短期配给相关的信息?
    • 是的,那里有一些与short相关的字段。但是您需要解析 JSON。我会更新我的答案
    【解决方案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 中搜索并搜索“Stock”。

    【讨论】:

      猜你喜欢
      • 2017-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-05
      • 2013-01-04
      相关资源
      最近更新 更多