繁体   English   中英

Python - 查找 REST API 的总页数

[英]Python - Finding total number of pages of a REST API

我正在尝试遍历 REST API 并获取完整的数据集。

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)

以上返回page 1数据,我可以通过在 URL 中指定页码来逐页地为所有其他页面执行此操作。 我不确定如何找到总页数,以便我可以对 API 提要中的所有页执行 for 循环。

我能够使用以下代码获得页数:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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