Home > Mobile >  Code querying a website multiple times not working?
Code querying a website multiple times not working?

Time:08-09

Sorry for my limited python knowledge.

I was using this code:

import requests

symbols = ["XYZW","XYZW","ABC"]
for s in symbols:
    url = 'https://www.alphavantage.co/query?function=BALANCE_SHEET&symbol={}&apikey=apikey'.format(s)

r = requests.get(url)
data = r.json()

And expected an output of the three different dictionaries, but only got the ABC's data. Am I supposed to loop it? I'm not sure how to. And why did it give me the last in the list? Does it sort alphabetically?

CodePudding user response:

Use a list to store the value on each iteration, and then loop through them to print the results.

import requests

symbols = ["XYZW","XYZW","ABC"]
urls = []
for s in symbols:
    urls.append('https://www.alphavantage.co/query?function=BALANCE_SHEET&symbol={}&apikey=apikey'.format(s))

for url in urls:
    r = requests.get(url)
    data = r.json()
    print(data)

CodePudding user response:

you reset the url every iteration of your for loop. Therefore you are only requesting the last url in the list.

  • Related