D
D
din44rx2020-12-17 19:54:09
Python
din44rx, 2020-12-17 19:54:09

How to make pagination (iterator) through API pages?

There is an API that only returns 100 results per page. I'm trying to make a while loop so that it goes through all the pages and takes the results from all the pages, but it doesn't work. I would be grateful if you could help me figure it out.

The code:

params = dict(
    order_by='salary_desc',
    text=keyword,
    area=area,
    period=30, # days
    per_page=100,
    page = 0,
    no_magic='false',  # disable magic
    search_field='name'  # available: name, description, company_name
)
response = requests.get(
    BASE_URL + '/vacancies',
    headers={'User-Agent': generate_user_agent()},
    params=params,
)
response

items = response.json()['items']
vacancies = []
for item in items:
    vacancies.append(dict(
        id=item['id'],
        name=item['name'],
        salary_from=item['salary']['from'] if item['salary'] else None,
        salary_to=item['salary']['to'] if item['salary'] else None,
        currency = item['salary']['currency'] if item['salary'] else None,
        created=item['published_at'],
        company=item['employer']['name'],
        area = item['area']['name'],
        url=item['alternate_url']
    ))


I loop through the dictionary, if there is a result in the dictionary, I add the iterator +1 to the page parameter:

while vacancies == True:
  params['page'] += 1


The result in the params['page'] = dictionary remains zero (pages in the API start from zero).

When calling params after running the loop, the result is:
{'area': 1,
'no_magic': 'false',
'order_by': 'salary_desc',
'page': 0,
'per_page': 100,
'period': 30,
' search_field': 'name',
'text': '"python"'}

Perhaps I'm doing the loop myself wrong, starting from the logic that while there is a result in the dictionary, the loop should be executed.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
Wispik, 2020-12-17
@Wispik

well, the request to the api also needs to be done in a cycle, and not just increase the page

G
gromyko21, 2020-12-18
@gromyko21

Something along those lines. This example will print each vacancy until they run out of list

for i in vacancies:
   print(i)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question