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.
There already exists a Python library for the Open Movie Database API, called omdb.
Install it with pip.
In [ ]:
!pip install omdb
In [ ]:
# Import the library.
import omdb
In [ ]:
# Search for movies.
movies = omdb.search("Westworld")
In [ ]:
movies
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"])
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"])
We used movie["title"] to show the title of the movie. Show something else! The documentation says you can get this info:
titleyeartypeactorsawardscountrydirectorgenreepisodeseasonseries_idlanguagemetascoreplotposterratedreleasedruntimewriterimdb_idimdb_ratingimdb_votesbox_officedvdproductionwebsitetomato_consensustomato_freshtomato_imagetomato_metertomato_ratingtomato_reviewstomato_rottentomato_user_metertomato_user_ratingtomato_user_reviews
In [ ]:
# Modify this.
movie = omdb.title("Westworld", year="2016")
print(movie["title"])