通过列表中的python url循环变量

时间:2017-10-10 20:46:25

标签: python loops url

我可以在我的网址中获得一个股票代码...但是如何创建一个列表并循环访问该网址。请查看失败的尝试...这是一个我正在摆弄的项目。主要是担心通过多个网址。

ticker=["AAPL","XOM"]
For i < len(ticker):


    responeData = requests.get("https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol="+str(ticker[i])+"&apikey=XXXXXX")
    symbol = str(responeData.json()['Meta Data']['2. Symbol'])
    refresh = str(responeData.json()['Meta Data']['3. Last Refreshed'])
    checkclose = str(responeData.json()['Time Series (Daily)'])
    close=str(responeData.json()['Time Series (Daily)'][refresh]['4. close'])


    api.update_status(status=symbol+' '+refresh+' Close Price: $'+close)

1 个答案:

答案 0 :(得分:0)

您可以使用for循环来迭代列表。

tickers=["AAPL","XOM"]
for ticker in tickers:

    responeData = requests.get("https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol="+ticker+"&apikey=XXXXXX")
    symbol = str(responeData.json()['Meta Data']['2. Symbol'])
    refresh = str(responeData.json()['Meta Data']['3. Last Refreshed'])
    checkclose = str(responeData.json()['Time Series (Daily)'])
    close=str(responeData.json()['Time Series (Daily)'][refresh]['4. close'])

    api.update_status(status=symbol+' '+refresh+' Close Price: $'+close)