如何直接从Python使用Alpha Vantage API

sha*_*odh 2 python api python-requests alphavantage

我一直在使用ROMEL托雷斯的alpha_vantage包,但也想从蟒蛇直接使用阿尔法华帝API(提供了更大的功能)与包请求作为这里所描述的卷曲通过Python的API调用

import requests
import alpha_vantage

API_URL = "https://www.alphavantage.co/query"

data = {
    "function": "TIME_SERIES_DAILY",
    "symbol": "NIFTY",
    "outputsize": "compact",
    "datatype": "csv"
    "apikey": "XXX",
    }
response = requests.get(API_URL, data)
print(response.json())[/code]
Run Code Online (Sandbox Code Playgroud)

但是正在返回的字典中收到以下错误消息:

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

使用requests.post(),结果为:

response = requests.post(API_URL, data)
{'detail': 'Method "POST" not allowed.'}
Run Code Online (Sandbox Code Playgroud)

I've re-checked the documentation and am following all the required API parameters. Appreciate some help re what I might be missing here and what the correct call would be and/or any other alternative approach. Thanks

I_g*_*guy 5

提示中有错误。将您的请求方法从更改postget

response = requests.get(API_URL, params=data)
Run Code Online (Sandbox Code Playgroud)

并使用作为Alpha Vantage数据存在的股票代码。 NIFTY不是股票-它是指数。如果您尝试使用编写代码MSFT,它将起作用。


Ser*_*nko 5

这就是我在不使用任何包装器的情况下从 Alpha Vantage 获取每日股票时间序列的方法。接收后,我将数据转换为 pandas 数据框以进行进一步处理。

    import requests
    import pandas as pd

    API_URL = "https://www.alphavantage.co/query" 
    symbol = 'SMBL'

    data = { "function": "TIME_SERIES_DAILY", 
    "symbol": symbol,
    "outputsize" : "full",
    "datatype": "json", 
    "apikey": "your_api_key" } 

    response = requests.get(API_URL, data) 
    response_json = response.json() # maybe redundant

    data = pd.DataFrame.from_dict(response_json['Time Series (Daily)'], orient= 'index').sort_index(axis=1)
    data = data.rename(columns={ '1. open': 'Open', '2. high': 'High', '3. low': 'Low', '4. close': 'Close', '5. adjusted close': 'AdjClose', '6. volume': 'Volume'})
    data = data[[ 'Open', 'High', 'Low', 'Close', 'AdjClose', 'Volume']]
    data.tail() # check OK or not
Run Code Online (Sandbox Code Playgroud)