Home > other >  How to load data from list of API links in Python?
How to load data from list of API links in Python?

Time:07-14

I have a list of API links, and I'm trying to get the data from these API links.

If my list of API links looks like this:

api_links = ['https://api.blahblah.com/john', 'https://api.blahblah.com/sarah', 'https://api.blahblah.com/jane']

How can I get a list of loaded data from these API links? I'm getting an error message when doing this code:

response_API = requests.get([(x) for x in api_links])

Which is preventing me from loading the data here:

data = response_API.text
data_lst = json.loads(data)

Where am I going wrong? Would appreciate any help - thanks!

CodePudding user response:

change

response_API = requests.get([(x) for x in api_links])

to

response_API = [requests.get(x) for x in api_links]

responce_api will be a dict of requests object.

CodePudding user response:

The function requests.get take as first argument an URL, not a list of them.

You want to call this function several times with one string, instead of one time with a list of strings.

Like this with a for loop :

for api_link in api_links:
    response_API = requests.get(api_link)
    data = response_API.text
    data_lst = json.loads(data)
    # Process further the data for the current api_link 

The use of comprehension lists may not be a good idea here, as the process to do on each API link is not trivial.

  • Related