Consuming API

Instead of using web scraping, using an API, application programming interface, is the preferred method.

Usually, you need to register in order to use an API. But we will use a freely available API called Open Movie Database API.

1. Install Python library

There already exists a Python library for the Open Movie Database API, called omdb.

Install it with pip.


In [ ]:
!pip install omdb

2. Access the API


In [ ]:
# Import the library.
import omdb

In [ ]:
# Search for movies.
movies = omdb.search("Westworld")

In [ ]:
movies

3. Present results


In [ ]:
# Since "movies" is a list, we can loop through it.
for movie in movies:
    print("Title: " + movie["title"])
    print("Type: " + movie["type"])
    print("Year: " + movie["year"])
    print("Link: http://imdb.com/title/" + movie["imdb_id"])
    print()

In [ ]:
# Lets pick the first movie (remember, lists always start at 0).
movie = movies[0]

print(movie["title"])
print(movie["year"])

4. Get more info


In [ ]:
# Instead of searching (and get a list of movies), we can specify the movie we want (and get a single movie).
movie = omdb.title("Westworld", year="2016")

In [ ]:
movie

In [ ]:
# Present some info about this movie.
print(movie["title"])
print(movie["year"])
print(movie["country"])
print("Rating: " + movie["imdb_rating"])
print(movie["plot"])

Exercise

  1. Pick your favorite movie or tv-series, and search for it.
  2. We used movie["title"] to show the title of the movie. Show something else! The documentation says you can get this info:

    • title
    • year
    • type
    • actors
    • awards
    • country
    • director
    • genre
    • episode
    • season
    • series_id
    • language
    • metascore
    • plot
    • poster
    • rated
    • released
    • runtime
    • writer
    • imdb_id
    • imdb_rating
    • imdb_votes
    • box_office
    • dvd
    • production
    • website
    • tomato_consensus
    • tomato_fresh
    • tomato_image
    • tomato_meter
    • tomato_rating
    • tomato_reviews
    • tomato_rotten
    • tomato_user_meter
    • tomato_user_rating
    • tomato_user_reviews

In [ ]:
# Modify this.
movie = omdb.title("Westworld", year="2016")
print(movie["title"])