0

我尝试了两种方法,并且都显示了我在这张图片中给出的不同位置

 apikey='abcd'
 import pandas as pd 
 from alpha_vantage.timeseries import TimeSeries
 import time
 ts=TimeSeries(key=apikey,output_format='pandas')
 


data,metadata=ts.get_intraday(symbol='name',interval='1min',outputsize='full')
 data

 while True:
     data, metadata=ts.get_intraday(symbol='TCS',interval='1min',outputsize='full')
     data.to_excel('livedat.xlsx')
     time.sleep(60)

代码运行正常,但我不知道如何在 excel 中获取数据文件。imp-该方法应该得到及时更新的文件,即自动1分钟。另外我正在使用 IBM watson studio 编写代码。

4

1 回答 1

0

我不熟悉您正在使用的 alpha_vantage 包装器,但这就是我将如何执行您的问题。该代码有效,我已包含评论。

要在 python 脚本中获取文件,我会执行 pd.read_excel(filepath)。

import requests
import pandas as pd
import time
import datetime

# Your API KEY and the URL we will request from
API_KEY = "YOUR API KEY"
url = "https://www.alphavantage.co/query?"


def Generate_file(symbol="IBM", interval="1min"):
    # URL parameters
    parameters = {"function": "TIME_SERIES_INTRADAY",
                  "symbol": symbol,
                  "interval": interval,
                  "apikey": API_KEY,
                  "outputsize": "compact"}

    # get the json response from AlphaVantage
    response = requests.get(url, params=parameters)
    data = response.json()

    # filter the response to only get the time series data we want
    time_series_interval = f"Time Series ({interval})"
    prices = data[time_series_interval]

    # convert the filtered reponse to a Pandas DataFrame
    df = pd.DataFrame.from_dict(prices, orient="index").reset_index()
    df = df.rename(columns={"index": time_series_interval})

    # create a timestampe for our excel file. So that the file does not get overriden with new data each time.
    current_time = datetime.datetime.now()
    file_timestamp = current_time.strftime("%Y%m%d_%H.%M")
    filename = f"livedat_{file_timestamp}.xlsx"
    df.to_excel(filename)

# sent a limit on the number of calls we make to prevent infinite loop

call_limit = 3
number_of_calls = 0
while(number_of_calls < call_limit):
    Generate_file()  # our function
    number_of_calls += 1
    time.sleep(60)
于 2020-07-14T17:46:17.207 回答