【问题标题】:Alpha Vantage passing multiple queries - Global QuoteAlpha Vantage 传递多个查询 - 全球报价
【发布时间】:2019-12-28 06:01:30
【问题描述】:

我正在尝试将 Alpha Vantage 查询中的值传递给 pandas df,以便我可以提取某些值。

当我运行一个简单的 1 个符号查询时,我可以轻松地将信息传递到 df.之后,我可以转置 df 并使用列提取数据。

一个例子是:

import requests
import alpha_vantage
import pandas as pd

API_URL = "https://www.alphavantage.co/query"
data = {
    "function": "GLOBAL_QUOTE",
    "symbol": "MSFT",
    "apikey": "XXX",
    }

response = requests.get(API_URL, params=data)
print(response.json())

df = pd.DataFrame(response.json())
df = df.T
print()
print()
print(df)

这会返回:

但是,我希望传递多个符号,但一直收到错误消息:

{'错误消息': '无效的 API 调用。请重试或访问 GLOBAL_QUOTE 的文档 (https://www.alphavantage.co/documentation/)。'}

正在使用的代码是:

import requests
import alpha_vantage
import pandas as pd

df = pd.DataFrame()
API_URL = "https://www.alphavantage.co/query"

symbols= ["IBM", "MSFT", "APPL"]
for symbol in symbols:
    data = {
        "function": "GLOBAL_QUOTE",
        "symbol": symbols,
        "apikey": "XXX",
        }

    response = requests.get(API_URL, params=data)

    print(response.json())

我认为这与网站预期的格式相对应:

https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=MSFT&apikey=demo

我的问题是:如何使用符号列表将多个请求传递给 Alpha Vantage API?

【问题讨论】:

    标签: python python-3.x alpha-vantage


    【解决方案1】:

    将“符号”更改为“符号”

    "symbol": symbols, -> "symbol": symbol, 现在您将整个列表作为数据传递。

    import alpha_vantage
    import pandas as pd
    
    df = pd.DataFrame()
    API_URL = "https://www.alphavantage.co/query"
    
    symbols= ["IBM", "MSFT", "APPL"]
    for symbol in symbols:
        data = {
            "function": "GLOBAL_QUOTE",
            "symbol": symbol,
            "apikey": "XXX",
            }
    
        response = requests.get(API_URL, params=data)
    
        print(response.json())
    

    【讨论】:

      【解决方案2】:

      您必须使用 BATCH_STOCK_QUOTES 并使用字符串而不是数组,应该这样做:

      import requests
      import alpha_vantage
      import pandas as pd
      
      df = pd.DataFrame()
      API_URL = "https://www.alphavantage.co/query"
      
      #symbols= ["IBM", "MSFT", "LVLT"]
      symbols= "IBM,MSFT,LVLT"
      data = {
              "function": "BATCH_STOCK_QUOTES",
              "symbols": symbols,
              "apikey": "XXX",
              }
      
      response = requests.get(API_URL, params=data)
      
      
      print(response.json())
      
      

      http://alpha.vantage.api.gallery.streamdata.io/listings/alpha-vantage/queryfunctionbatch-stock-quotes-get-openapi/

      【讨论】:

      • 这个方法好像已经停止了。此外,它提供的信息比我需要的要少。
      猜你喜欢
      • 1970-01-01
      • 2020-02-22
      • 1970-01-01
      • 2022-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-31
      相关资源
      最近更新 更多