lundi 7 février 2022

Is it possible to load all data of a paginated API with only one request(Python)?

I'm retrieving data from a API in python that randomize its result, but the entire result is paginated. What happens is that sometimes, the same element appears in different pages. So I end up getting one element several times, and others are missing.

I thought that using session would solve this, but it doesn't.

def get_result(data=data, headers=headers):
    current_page_number = 1
    url = compose_url(current_page_number)
    s = requests.Session()
    response = s.get(url, headers=headers, data=json.dumps(data), verify=False)
    resp =  response.json()
    total_pages = resp['total_pages']
    all_info = []
    for i in range(1, total_pages+1):
        url = compose_url(current_page_number)
        response = s.get(url, headers=headers, data=json.dumps(data), verify=False)
        resp = response.json()
        elements = resp['elements']
        for element in elements:
            all_info.append(do_something_with_element(element))
        current_page_number += 1
        
    return all_info

Is there a way to make only one request that loads all data of the paginated result, so I don't have to request again the server (because it causes the API to randomize the response)?




Aucun commentaire:

Enregistrer un commentaire