# Run this cell without changes
import requests
import json
from bs4 import BeautifulSoup
In this assessment you will primarily be exploring a Pokemon dataset. Pokemon are fictional creatures from the Nintendo franchise of the same name.
Some Pokemon facts that might be useful:
- The word "pokemon" is both singular and plural. You may refer to "one pokemon" or "many pokemon".
- Pokemon have attributes such as a name, weight, and height.
- Pokemon have one or multiple "types". A type is something like "electric", "water", "ghost", or "normal" that indicates the abilities that pokemon may possess.
- The humans who collect pokemon are called "trainers".
We'll be using the PokéAPI to get data on Pokemon.
Consult the PokéAPI documentation here for information on obtaining data from this API.
We want to know the "types" of any particular pokemon given its name. Complete the get_pokemon_types
function below. It should return a list
of all the names of the "types" that pokemon has
Within get_pokemon_types
, make a request to "https://pokeapi.co/api/v2/pokemon/<add-name-of-pokemon-here>"
. Inspect the API response and extract the names of the types. Here are the docs for this specific API route.
Example usage of get_pokemon_types
:
get_pokemon_types("pikachu") # returns ["electric"]
get_pokemon_types("bulbasaur") # returns ["poison", "grass"]
get_pokemon_types("snorlax") # returns ["normal"]
get_pokemon_types("moltres") # returns ["flying", "fire"]
# Replace None with appropriate code
def get_pokemon_types(name):
'''
input: name - a string of the pokemon's name
return: a list of strings of the one or more types belonging to the pokemon
'''
# Make a request to "https://pokeapi.co/api/v2/pokemon/<add-name-of-pokemon-here>"
None
# Extract and return the names of the types
None
# Run this cell without changes to test your code
get_pokemon_types("bulbasaur")
The PokéAPI has lots of information about pokemon, but it doesn't have everything!
Use BeautifulSoup to find the designer(s) of a pokemon by scraping Wikipedia. (Note: this only works for pokemon that are "notable" enough to have a Wikipedia page.)
For example, this is the Wikipedia page for Pikachu. Before writing the following function, make sure you go to the site and inspect the page. Look at the structure of the HTML elements in the info box (highlighted in red in the image below).
Complete the get_pokemon_designers
function below. It should return a string representing the designer(s) of the pokemon. We have already completed the initial request for you.
There are four steps to extract this information from the soup
object:
- Find the
info_box
by searchingsoup
for atable
HTML tag withclass
"infobox"
- Find the
fifth_row
by searchinginfo_box
fortr
HTML tags and selecting the fifth one (index 4) - Find the
table_cell
by searchingfifth_row
for atd
HTML tag - Find the
designers
by extracting thetext
fromtable_cell
Example usage of get_pokemon_designers
:
get_pokemon_designers("pikachu") # returns 'Atsuko Nishida and Ken Sugimori'
get_pokemon_designers("bulbasaur") # returns 'Atsuko Nishida[1]'
get_pokemon_designers("snorlax") # returns 'Ken Sugimori'
get_pokemon_designers("moltres") # returns 'Ken Sugimori'
# Replace None with appropriate code
def get_pokemon_designers(name):
'''
input: name - a string of the pokemon's name
return: a string representing the designer(s) of the pokemon
'''
wikipedia_page = requests.get(f"https://en.wikipedia.org/wiki/{name}")
soup = BeautifulSoup(wikipedia_page.content, "html.parser")
# Search soup for a table HTML tag with class "infobox"
info_box = None
# Search info_box for tr HTML tags and select the fifth one (index 4)
fifth_row = None
# Search fifth_row for a td HTML tag
table_cell = None
# Extract the text from table_cell
designers = None
return designers
# Run this cell without changes to test your code
get_pokemon_designers("pikachu")