Giter Site home page Giter Site logo

011121-api-checkpoint's Introduction

APIs and Web Scraping

# 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".

1. Accessing Data Through APIs

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")

2. Accessing Data Through Web Scraping

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).

pikachu wikipedia page

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:

  1. Find the info_box by searching soup for a table HTML tag with class "infobox"
  2. Find the fifth_row by searching info_box for tr HTML tags and selecting the fifth one (index 4)
  3. Find the table_cell by searching fifth_row for a td HTML tag
  4. Find the designers by extracting the text from table_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")

011121-api-checkpoint's People

Contributors

hoffm386 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.