简体   繁体   中英

Python - Finding total number of pages of a REST API

I am trying to loop through a REST API and fetch the complete data set.

url = f'https://apiurl.com/api/1.1/json/tickets?page=1'
auth = (f'{api_key}', f'{auth_code}')
res = requests.get(url, auth=auth)
data = json.loads(res.content)

The above returns data for page 1 and I am able to do it for all other pages, page by page by specifying the page number in the URL. I am not sure how do I find the total pages such that I can perform a for loop that does it for all pages in the API feed.

I was able to get the number of pages using the below code:

res = requests.get(url, auth=auth)
data=res.json()

while 'next' in res.links.keys():
    res = requests.get(res.links['next']['url'])
    data.extend(res.json())


page_count = repos['page_info']['page_count'] <<-- This returns the max page count

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