|
| 1 | +import requests |
| 2 | +from bs4 import BeautifulSoup |
| 3 | + |
| 4 | +def get_website_content(url): |
| 5 | + response = requests.get(url) |
| 6 | + status = response.status_code |
| 7 | + if status == 200: |
| 8 | + print("La petición fue correcta") |
| 9 | + soup = BeautifulSoup(response.content, 'html.parser') |
| 10 | + print(soup.prettify()) |
| 11 | + else: |
| 12 | + print(f"Petición fallida, código: {status}") |
| 13 | + |
| 14 | +def get_pokemon_data(name_or_id): |
| 15 | + try: |
| 16 | + response = requests.get(f'https://pokeapi.co/api/v2/pokemon/{name_or_id}') |
| 17 | + response.raise_for_status() |
| 18 | + data = response.json() |
| 19 | + name = data["name"].capitalize() |
| 20 | + id = data["id"] |
| 21 | + weight = data["weight"] / 10 |
| 22 | + height = data["height"] / 10 |
| 23 | + types = [type["type"]["name"].capitalize() for type in data["types"]] |
| 24 | + evolution_chain_url = data["species"]["url"] |
| 25 | + evolution_chain_response = requests.get(evolution_chain_url) |
| 26 | + evolution_chain_response.raise_for_status() |
| 27 | + evolution_chain_data = evolution_chain_response.json() |
| 28 | + |
| 29 | + if "chain" in evolution_chain_data and evolution_chain_data["chain"]: |
| 30 | + evolution_chain = [pokemon["species"]["name"].capitalize() for pokemon in evolution_chain_data["chain"]["evolves_to"]] |
| 31 | + else: |
| 32 | + evolution_chain = [name] |
| 33 | + game_appearances = [game["version"]["name"] for game in data["game_indices"]] |
| 34 | + |
| 35 | + pokemon_info = f''' |
| 36 | + POKEMON DATA: |
| 37 | + -Name: {name} |
| 38 | + -Id: {id} |
| 39 | + -Weight: {weight} kg |
| 40 | + -Height: {height} m |
| 41 | + -Type(s): {', '.join(types)} |
| 42 | + -Evolution chain: {' to '.join(evolution_chain)} |
| 43 | + -Games: {', '.join(game_appearances)} |
| 44 | + ''' |
| 45 | + return pokemon_info |
| 46 | + except requests.HTTPError as err: |
| 47 | + return f'Error al obtener datos: {err}' |
| 48 | + |
| 49 | +if __name__ == "__main__": |
| 50 | + url = 'https://retosdeprogramacion.com/' |
| 51 | + get_website_content(url) |
| 52 | + pokemon_name_or_id = input("Enter the name or number of the Pokémon: ") |
| 53 | + pokemon_info = get_pokemon_data(pokemon_name_or_id) |
| 54 | + print(pokemon_info) |
0 commit comments