【问题标题】:Pagination loop with offset 100偏移量为 100 的分页循环
【发布时间】:2019-08-23 03:49:39
【问题描述】:

我正在编写从 API 获取记录的代码,并且该 API 在其上实现了分页,最多允许 100 条记录。所以我必须循环100的倍数。目前,我的代码比较从 offset 100 到 101,102,103 等的总记录和循环。我希望它循环 100 个(如 100,200,300),并在偏移量大于总记录时立即停止。我不知道该怎么做,我有部分代码递增 1 而不是 100,并且在需要时不会停止。谁能帮我解决这个问题。

import pandas as pd
from pandas.io.json import json_normalize

#Token for Authorization
API_ACCESS_KEY = 'Token'
Accept='application/xml'

#Query Details that is passed in the URL
since = '2018-01-01'
until = '2018-02-01'
limit = '100'
offset = '0'
total = 'true'

def get():

    url_address = "https://mywebsite/web?offset="+str('0') 
    headers = {
        'Authorization': 'token={0}'.format(API_ACCESS_KEY),
        'Accept': Accept,
    }
    querystring = {"since":since,"until":until, "limit":limit, "total":total}


    # find out total number of pages
    r = requests.get(url=url_address, headers=headers, params=querystring).json()
    total_record = int(r['total'])
    print("Total record: " +str(total_record))

    # results will be appended to this list
    all_items = []

    # loop through all offset and return JSON object
    for offset in range(0, total_record):

        url = "https://mywebsite/web?offset="+str(offset)              
        response = requests.get(url=url, headers=headers, params=querystring).json()        
        all_items.append(response)       
        offset = offset + 100
        print(offset)

    # prettify JSON
    data = json.dumps(all_items, sort_keys=True, indent=4)

    return data

print(get())

目前当我打印我看到的偏移量
总记录:345
100,
101,
102、

预期:
总记录:345
100,
200,
300
停止循环!

【问题讨论】:

    标签: python api pagination


    【解决方案1】:

    你可以做到的一种方法是改变

    for offset in range(0, total_record):
        url = "https://mywebsite/web?offset="+str(offset)              
        response = requests.get(url=url, headers=headers, params=querystring).json()        
        all_items.append(response)       
        offset = offset + 100
        print(offset)
    

    for offset in range(0, total_record, 100):
        url = "https://mywebsite/web?offset="+str(offset)              
        response = requests.get(url=url, headers=headers, params=querystring).json()        
        all_items.append(response)       
        print(offset)
    

    因为你不能改变循环内的偏移量

    【讨论】:

    • 太棒了 非常感谢@dangee1705 它就像一个魅力没有问题。它一达到 300 就停止了。我最后一个问题是如何将所有结果放入数据框中?我知道结果附加在 all_items 中。
    【解决方案2】:

    遍历所有偏移并返回 JSON 对象

    for offset in range(0,total_record,100):
    
        url = "https://mywebsite/web?offset="+str(offset)              
        response = requests.get(url=url, headers=headers, params=querystring).json()        
        all_items.append(response)       
        print(offset)
    

    【讨论】:

    • 这不起作用,你不能修改循环内的偏移量
    • 是的 Shivangi 我尝试了改变偏移量的解决方案,但它并没有停止循环。
    • 抱歉,我错过了您问题的第二部分。试试新代码
    • 不用担心,新代码有效,如果可能,我还有最后一个问题请帮助我如何将所有结果放入数据框中?我知道所有记录(345)都附加在 all_items 中,但我如何将其传输到数据框
    • 你可以使用 pandas 做到这一点
    猜你喜欢
    • 2013-08-24
    • 2011-06-20
    • 2015-07-23
    • 1970-01-01
    • 1970-01-01
    • 2020-05-23
    • 1970-01-01
    • 2012-01-07
    • 2021-10-14
    相关资源
    最近更新 更多