|
| 1 | +## Ejercicio |
| 2 | + |
| 3 | +import requests |
| 4 | +from bs4 import BeautifulSoup |
| 5 | + |
| 6 | +url = "https://www.google.com" |
| 7 | + |
| 8 | +response = requests.get(url) |
| 9 | + |
| 10 | +if response.status_code == 200: |
| 11 | + print("La petición fue exitosa!") |
| 12 | + |
| 13 | + soup = BeautifulSoup(response.text, 'html.parser') |
| 14 | + |
| 15 | + print(soup.prettify()) |
| 16 | +else: |
| 17 | + print(f"La petición falló con el código de estado: {response.status_code}") |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | +## Dificultad extra |
| 23 | + |
| 24 | + |
| 25 | +import requests |
| 26 | + |
| 27 | +def get_pokemon_info(pokemon_name_or_id): |
| 28 | + try: |
| 29 | + response = requests.get(f"https://pokeapi.co/api/v2/pokemon/{pokemon_name_or_id}") |
| 30 | + response.raise_for_status() |
| 31 | + |
| 32 | + pokemon_data = response.json() |
| 33 | + |
| 34 | + name = pokemon_data["name"].capitalize() |
| 35 | + pokemon_id = pokemon_data["id"] |
| 36 | + weight = pokemon_data["weight"] / 10 |
| 37 | + height = pokemon_data["height"] / 10 |
| 38 | + types = [t["type"]["name"].capitalize() for t in pokemon_data["types"]] |
| 39 | + evolution_chain_url = pokemon_data["species"]["url"] |
| 40 | + game_appearances = [entry["version"]["name"] for entry in pokemon_data["game_indices"]] |
| 41 | + |
| 42 | + evolution_chain_response = requests.get(evolution_chain_url) |
| 43 | + evolution_chain_response.raise_for_status() |
| 44 | + evolution_chain_data = evolution_chain_response.json() |
| 45 | + |
| 46 | + if "chain" in evolution_chain_data and evolution_chain_data["chain"]: |
| 47 | + evolution_chain = [pokemon["species"]["name"].capitalize() for pokemon in evolution_chain_data["chain"]["evolves_to"]] |
| 48 | + else: |
| 49 | + evolution_chain = [name] |
| 50 | + |
| 51 | + print(f"Nombre: {name}") |
| 52 | + print(f"ID: {pokemon_id}") |
| 53 | + print(f"Peso: {weight} kg") |
| 54 | + print(f"Altura: {height} m") |
| 55 | + print(f"Tipo(s): {', '.join(types)}") |
| 56 | + print(f"Cadena de evolución: {' -> '.join(evolution_chain)}") |
| 57 | + print(f"Aparece en los juegos: {', '.join(game_appearances)}") |
| 58 | + |
| 59 | + except requests.exceptions.RequestException as e: |
| 60 | + print(f"Error al realizar la petición: {e}") |
| 61 | + except ValueError: |
| 62 | + print("El valor proporcionado no es un nombre o número de Pokémon válido.") |
| 63 | + except Exception as e: |
| 64 | + print(f"Error inesperado: {e}") |
| 65 | + |
| 66 | +# Solicitar al usuario el nombre o número del Pokémon |
| 67 | +pokemon_name_or_id = input("Ingresa el nombre o número del Pokémon: ").lower() |
| 68 | + |
| 69 | +# Llamar a la función para obtener la información del Pokémon |
| 70 | +get_pokemon_info(pokemon_name_or_id) |
0 commit comments