-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
28 lines (22 loc) · 861 Bytes
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import requests
from bs4 import BeautifulSoup
def get_pokemon_name(pokemon_id):
url = f"https://pokeapi.co/api/v2/pokemon/{pokemon_id}"
response = requests.get(url)
if response.status_code == 200:
pokemon_data = response.json()
pokemon_name = pokemon_data["name"]
return pokemon_name
else:
return None
def get_pokemon_name_by_scraping(pokemon_id):
url = "https://pokemon.gameinfo.io/es"
response = requests.get(url)
soup = BeautifulSoup(response.content, "html.parser")
pokemon_list = soup.find("div", class_="pokemon-list").find_all("div", class_="gen")
pokemon_name = None
for pokemon in pokemon_list:
id = pokemon.find("div", class_="id").text.strip()
if id == pokemon_id:
pokemon_name = pokemon.find("h2").text.strip().lower()
return pokemon_name