-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3656 from CesarCarmona30/main
#20 - Python
- Loading branch information
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import requests | ||
from bs4 import BeautifulSoup | ||
|
||
def get_website_content(url): | ||
response = requests.get(url) | ||
status = response.status_code | ||
if status == 200: | ||
print("La petición fue correcta") | ||
soup = BeautifulSoup(response.content, 'html.parser') | ||
print(soup.prettify()) | ||
else: | ||
print(f"Petición fallida, código: {status}") | ||
|
||
def get_pokemon_data(name_or_id): | ||
try: | ||
response = requests.get(f'https://pokeapi.co/api/v2/pokemon/{name_or_id}') | ||
response.raise_for_status() | ||
data = response.json() | ||
name = data["name"].capitalize() | ||
id = data["id"] | ||
weight = data["weight"] / 10 | ||
height = data["height"] / 10 | ||
types = [type["type"]["name"].capitalize() for type in data["types"]] | ||
evolution_chain_url = data["species"]["url"] | ||
evolution_chain_response = requests.get(evolution_chain_url) | ||
evolution_chain_response.raise_for_status() | ||
evolution_chain_data = evolution_chain_response.json() | ||
|
||
if "chain" in evolution_chain_data and evolution_chain_data["chain"]: | ||
evolution_chain = [pokemon["species"]["name"].capitalize() for pokemon in evolution_chain_data["chain"]["evolves_to"]] | ||
else: | ||
evolution_chain = [name] | ||
game_appearances = [game["version"]["name"] for game in data["game_indices"]] | ||
|
||
pokemon_info = f''' | ||
POKEMON DATA: | ||
-Name: {name} | ||
-Id: {id} | ||
-Weight: {weight} kg | ||
-Height: {height} m | ||
-Type(s): {', '.join(types)} | ||
-Evolution chain: {' to '.join(evolution_chain)} | ||
-Games: {', '.join(game_appearances)} | ||
''' | ||
return pokemon_info | ||
except requests.HTTPError as err: | ||
return f'Error al obtener datos: {err}' | ||
|
||
if __name__ == "__main__": | ||
url = 'https://retosdeprogramacion.com/' | ||
get_website_content(url) | ||
pokemon_name_or_id = input("Enter the name or number of the Pokémon: ") | ||
pokemon_info = get_pokemon_data(pokemon_name_or_id) | ||
print(pokemon_info) |