[Python] Python requests in an API, pagination only saves the last interation

Discussão em 'Python' iniciado por Stack, Abril 10, 2025.

  1. Stack

    Stack Membro Participativo

    I have this code below, using requests into an API :

    import requests

    def Sults():
    headers = {
    "Authorization":"xxxxxxxxxxxxxxxxxxxxxxxx",
    "Content-Type":"application/json;charset=UTF-8"
    }
    global id
    id = []
    for count1 in range(3):
    url = "https://api/api/v1/implantacao/projeto?&start={}&dtInicio=2022-01-01T18:02:55Z".format(count1)
    response = requests.get(url, headers=headers)
    data = response.json()
    url2 = "https://api/api/v1/implantacao/projeto?&dtInicio=2022-01-01T18:02:55Z&concluido=false"
    response2 = requests.get(url2, headers=headers)
    data2 = response2.json()
    url3 = "https://api/api/v1/implantacao/projeto?&dtInicio=2022-01-01T18:02:55Z&concluido=true"
    response3 = requests.get(url3, headers=headers)
    data3 = response3.json()

    # print(data)
    ids = unidades(data)
    print(ids)
    ids2 = unidades2(data2)
    print(ids2)
    ids3 = unidades3(data3)
    print(ids3)

    def unidades(data):
    for i in data['data']:
    id.append(i['id']) #append id list
    return id



    def unidades2(data2):
    id_exclude = []
    for j in data2['data']:
    id_exclude.append(j['id'])
    return id_exclude

    def unidades3(data3):
    id_conclude = []
    for k in data3['data']:
    id_conclude.append(k['id'])
    return id_conclude


    if __name__ == '__main__':
    Sults()



    Into the line : url = "https:///api/v1/implantacao/projeto?&start={}&dtInicio=2022-01-01T18:02:55Z".format(count1) >> this count loops 0,1,2 to the pagination into the api. The problem is that when im trying to save the id from every loop into de list : id =[], the code only saves the last loop, in this case, 3(thrid page of the loop into 0,1,2).

    for example : "https://api/api/v1/implantacao/projeto?&start=0&dtInicio=2022-01-01T18:02:55Z" Output : [122, 123, 124, 125, 126, 127, 129, 132, 133, 134, 135, 137, 138, 140, 144, 145, 146, 147, 149, 150, 151, 153, 154, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233]

    url = "https://api/api/v1/implantacao/projeto?&start=1&dtInicio=2022-01-01T18:02:55Z" output about the second page : [234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271]

    The output i want is to join both outputs into the id list (in this case page 0 and page 1) : [122, 123, 124, 125, 126, 127, 129, 132, 133, 134, 135, 137, 138, 140, 144, 145, 146, 147, 149, 150, 151, 153, 154, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271]

    Continue reading...

Compartilhe esta Página