Last active
April 26, 2023 08:56
-
-
Save jonathanhle/b4d1acf183c5c89bf36e71d43593a751 to your computer and use it in GitHub Desktop.
Quick paginator using requests_paginator for the Pokemon API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
###### Method 1 | |
from requests_paginator import RequestsPaginator | |
BASE = 'https://pokeapi.co/api/v2/pokemon' | |
def get_next_page(page): | |
body = page.json() | |
if body['next']: | |
return body['next'] | |
return None | |
# instantiate the paginator | |
pages = RequestsPaginator(BASE + '?offset=0&limit=100', get_next_page) | |
list_all_pokemon = [] | |
for page in pages: | |
print(f"calling {page.url}") | |
page.raise_for_status() | |
print(f"found {len(page.json()['results'])} results") | |
page_results = page.json()["results"] | |
for pokemon in page_results: | |
list_all_pokemon.append(pokemon["name"]) | |
print(list_all_pokemon) | |
###### Method 2 | |
import requests | |
url = "https://pokeapi.co/api/v2/pokemon?limit=100" | |
all_pokemon = [] | |
while url: | |
response = requests.get(url) | |
data = response.json() | |
for pokemon in data["results"]: | |
all_pokemon.append(pokemon["name"]) | |
url = data['next'] | |
print((all_pokemon)) | |
###### 3 | |
import requests | |
class PokeAPIPaginator: | |
def __init__(self, endpoint, params=None, per_page=100): | |
self.base_url = 'https://pokeapi.co/api/v2' | |
self.endpoint = endpoint | |
self.params = params if params is not None else {} | |
self.per_page = per_page | |
self.page = 1 | |
def __iter__(self): | |
return self | |
def __next__(self): | |
offset = (self.page - 1) * self.per_page | |
self.params.update({ | |
'offset': offset, | |
'limit': self.per_page, | |
}) | |
response = requests.get(f'{self.base_url}{self.endpoint}', params=self.params) | |
response.raise_for_status() | |
data = response.json() | |
items = data['results'] | |
if not items: | |
raise StopIteration | |
self.page += 1 | |
return items | |
all_pokemon = [] | |
endpoint = '/pokemon' | |
paginator = PokeAPIPaginator(endpoint) | |
for items in paginator: | |
for item in items: | |
all_pokemon.append(item['name']) | |
print(all_pokemon) | |
# Other ways https://gist.github.com/jonathanhle/79e339ba25bf50d89d912c40e0ba9ff8 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment