【问题标题】:Iterate in a link and save the data迭代链接并保存数据
【发布时间】:2021-12-22 08:50:01
【问题描述】:

我想请教一下如何迭代链接并保存数据。

我正在使用 Alpha Vantage,他们只允许每个请求在 1 个月内进行日内分钟交易。为了获得第二个月,您必须更改链接中的 slice 参数。

我想创建一个 for 循环,该循环遍历我的列表并提取每个月的数据并将其保存在数据框中。以下是获取数据的步骤以及我到目前为止所做的事情。

这只给了我 year2month2 而不是整个时期。有人可以在这里指导我我做错了什么

# replace the "demo" apikey below with your own key from https://www.alphavantage.co/support/#api-key
symbol = 'pltr'
interval = '1min'
periods=['year1month1','year1month2','year1month3','year1month4','year1month5',
'year1month6','year1month7','year1month8','year1month9','year1month10',
'year1month11','year1month12','year2month1','year2month2','year2month3',
'year2month4','year2month5','year2month6','year2month7','year2month8',
'year2month9','year2month10','year2month11','year2month12']
df=[]
for period in periods: 
  df = pd.read_csv('https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY_EXTENDED&symbol='+ticker+'&interval=15min&slice='+period+'&apikey='+apiKey+'&datatype=csv&outputsize=full') 
  df.append(period)

#Show output
print(df)

【问题讨论】:

  • 我会使用pandas.read_csv 而不是csv.reader
  • 感谢您的回复,但没有成功

标签: python pandas for-loop alpha-vantage


【解决方案1】:

我能够获得如下的盘中数据,并想分享我的答案。

ticker = 'IBM'
periods=['year1month1','year1month2','year1month3','year1month4','year1month5','year1month6','year1month7','year1month8',
        'year1month9','year1month10','year1month11','year1month12','year2month1','year2month2','year2month3','year2month4'
        ,'year2month5','year2month6','year2month7','year2month8','year2month9','year2month10','year2month11','year2month12']
interval = '1min'


price_data = []
for i in periods:
    prices = pd.read_csv('https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY_EXTENDED&symbol='+ticker+'&interval='+interval+'&slice='+i+'&apikey='+apiKey+'&datatype=csv&outputsize=full') 
    price_data.append(prices)


data = pd.concat(price_data)
columns = ['Date_Time', 'open', 'high', 'low', 'close', 'volume']
data.columns = columns
data = data[1:]
data.set_index('Date_Time', inplace=True)  
data.index = pd.to_datetime(data.index)
data = data.iloc[::-1]
data = data.astype(float)
data

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-17
    • 2020-05-30
    • 2020-05-08
    • 2015-10-17
    • 1970-01-01
    • 2018-11-04
    • 2017-10-11
    • 1970-01-01
    相关资源
    最近更新 更多