简体   繁体   中英

Making ten api calls through a loop

An API I want to use limits requests to 10 items. I want to download 100 items. I am trying to write a function that makes 10 API, using their offset functionality to make it possible. I figured a loop would be the proper way to do this.

This is the code I have, but it doesn't work and I don't understand why:

import pandas as pd
import requests

api_key = 'THIS_IS_MY_KEY'
api_url = 'http://apiurl.com/doc?limit=10' # fake url
headers = {'Authorization': 'Bearer ' + api_key}

for x in range(0, 10):
    number = 0
    url = api_url + '&offset=' + str(number + 10)
    r = requests.get(url, headers=headers)
    x = pd.DataFrame(r.json())
    x = x['data'].apply(pd.Series)
return x

You are also using x as your loop counter and as your data frame - which i think is not good practice - although your code might still work because of the way that the for loop works. A better is to use the step parameter in the range call - as demonstrated below. It is also not clear what you are expecting to return - are you wanting to return the last offset you fetched - or the the data frame (since your code re-uses x in 3 different ways it is impossible to determine what you intended - so I left it as it is - although I am pretty sure it is wrong - looking at the panda API)

import pandas as pd
import requests

api_key = 'THIS_IS_MY_KEY'
api_url = 'http://apiurl.com/doc?limit=10' # fake url
headers = {'Authorization': 'Bearer ' + api_key}

for offset in range(0, 100, 10): # makes a list [0, 10,20,30,40,50,60,70,80,90,100]
    url = api_url + '&offset=' + str(offset)
    r = requests.get(url, headers=headers)
    x = pd.DataFrame(r.json())
    x = x['data'].apply(pd.Series) 
return x

what result do you see? try

url = api_url + '&offset=' + str(x * 10)

The variable number never change, since it is set to 0 at the start of the loop.
I think you means this:

import pandas as pd
import requests

api_key = 'THIS_IS_MY_KEY'
api_url = 'http://apiurl.com/doc?limit=10' # fake url
headers = {'Authorization': 'Bearer ' + api_key}
number = 0

for x in range(0, 10):
    url = api_url + '&offset=' + str(number + 10)
    r = requests.get(url, headers=headers)
    x = pd.DataFrame(r.json())
    x = x['data'].apply(pd.Series)
    number += 10

return x

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM