[英]How can I use Python requests to paginate api calls with offset parameter
I need to make repeated api calls as there is a limit on each call of 1000 records.我需要进行重复的 api 调用,因为每次调用 1000 条记录有限制。 There are about 20,000 total records of which I test them , keep a sample, then need request the next 1000. The offset parameter is available.我测试它们的总记录大约有 20,000 条,保留一个样本,然后需要请求下 1000 条。offset 参数可用。
p = getpass.getpass()
url = ("https://example.api.com/api/1.0/devices/all/?offset={}&include_cols=asset_no,name,service_level,building&type=physical"
r = requests.get(url, auth=HTTPBasicAuth('admin', p))
data = json.loads(r.text)
payload = data["Devices"]
I have to keep this very general since you did not give further details and mentioned no API vendor.我必须保持这个非常笼统,因为您没有提供更多细节,也没有提到 API 供应商。
Pagination can be done using a simple while
loop.分页可以使用一个简单的while
循环来完成。
The basic workflow is that while you are getting a pagination token in your response, keep making subsequent requests.基本工作流程是,当您在响应中获得分页令牌时,继续发出后续请求。 In pseudo code that might look like this:在可能如下所示的伪代码中:
Page = GetPageOfItems(); //process the data from the page, or add it to a larger array, etc. while( Page->cursor ) Page = GetPageOfItems(Page->cursor); //process the data again end
Ref: https://medium.com/square-corner-blog/tips-and-tricks-for-api-pagination-5cacc6f017da参考: https : //medium.com/square-corner-blog/tips-and-tricks-for-api-pagination-5cacc6f017da
The implementation also depends on API details, eg does the data header contain the current offset
and/or a hasMore
key, eg实现还取决于 API 细节,例如数据头是否包含当前offset
和/或hasMore
键,例如
p = getpass.getpass()
offset=0
while True:
url = ("https://example.api.com/api/1.0/devices/all/?offset=" + offset + "&include_cols=asset_no,name,service_level,building&type=physical"
r = requests.get(url, auth=HTTPBasicAuth('admin', p))
data = json.loads(r.text)
# Process the payload or add it to a list
offset = data['offset'] # offset +1?
hasMore = data['has-more']
if not hasMore:
break
In a general sense, pagination with limit
and offset
is a little formula :一般来说,带limit
和offset
的分页是一个小公式:
number_of_offsets = total_items / limit_per_page
The offset parameter controls the starting point within the collection of resource results. offset 参数控制资源结果集合内的起点。
If you have a collection of 15 items
to be retrieved from a resource and you specify limit=5
, you can retrieve the entire set of results in 3 successive requests by varying the offset value: offset=0
, offset=5
, and offset=10
.如果要从资源中检索15 items
的集合并指定limit=5
,则可以通过改变偏移值在 3 个连续请求中检索整个结果集: offset=0
、 offset=5
和offset=10
.
The number offsets become the number of pages.偏移量变成页数。 So in our example above it would be :因此,在我们上面的示例中,它将是:
offset = 0 : page 1
offset = 5 : page 2
offset = 10: page 3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.