How do you request on IMDB API in Python?

10,094

I have successfully used Flask for this exact thing several times. Using a framework makes things easy as all of the tools are already put together. You can read more about Flask here.

However, if you're comfortable using a more manual approach you can use the python json and requests module together to access a RESTful API with absolutely no problem.

An example might look like the following:

import json, requests

url = 'http://example.com/endpoint/'

parameters = dict(
    field1='some value',
    field2='another value',
    field3='yet another value',
)

resp = requests.get(url=url, params=parameters)
data = json.loads(resp.text)
print data

EDIT:

Looking closer at this problem I just realized that consuming the IMDB API is even easier than I thought, you just send a GET request with the parameters you are interested in. An actual working example of communicating with it to pull information about the movie "Scream" is as follows:

import json, requests
url = "http://www.omdbapi.com/?t=scream"
response = requests.get(url)
python_dictionary_values = json.loads(response.text)

The last line takes the json returned in the response and converts it to a Python dictionary which you are then free to use as you please.

If you want to do this multiple times with inputs from a file, you can certainly do this. My example assumes that you have a newline separated file and all the movies you are searching for have no spaces (if you need to handle spaces you will need to properly encode them in the URL - the code I have provided will break otherwise).

movies = {}
import json, requests
baseurl = "http://omdbapi.com/?t=" #only submitting the title parameter
with open("movies.txt", "r") as fin:
     for line in fin:
         movieTitle = line.rstrip("\n") # get rid of newline characters
         response = requests.get(url + movieTitle)
         if response.status_code == 200:
              movies[movieTitle] = json.loads(response.text)
         else:
              raise ValueError("Bad request!")
print movies['scream']
Share:
10,094
Chumbawoo
Author by

Chumbawoo

Updated on June 27, 2022

Comments

  • Chumbawoo
    Chumbawoo almost 2 years

    How do I submit a request on this API: http://www.omdbapi.com/

    I am trying to construct a spreadsheet using data from IMDB, such as title, year, budget, box office and genre. How do I get this information using the API in Python? I assume the data will be JSON, how do I parse through it to then pull our the information I want?

  • Chumbawoo
    Chumbawoo about 8 years
    Thanks! if I want to do multiple requests, is there a way to do that without manual input? For example, I have a text file with a list of movie names. Can I do something with that, how would I make for loop that could iterate through that and submit the requests? Or, is there a way to do one request but have all the movie titles requested? When I tried to separate by commas there was an error, I assume it is because the comma isn't recognized as a separator
  • MS-DDOS
    MS-DDOS about 8 years
    @Chumbawoo Updated my answer to show how you might do it using a simple text file. If you want to have multiple fields, you should probably look into using a CSV reader module or the python str.split() method. Hope this helps!
  • Chumbawoo
    Chumbawoo about 8 years
    Thank you that really helped! do you know how I can then take this json data and put it into a spreadsheet?
  • MS-DDOS
    MS-DDOS about 8 years
    You can manually write the dictionaries out to a file or you can use my one of my all time favorite python libraries tablib.