How do you get a thumbnail of a movie using IMDbPy?
Asked Answered
H

2

8

Using IMDbPy it is painfully easy to access movies from the IMDB site:

import imdb

access = imdb.IMDb()
movie = access.get_movie(3242) # random ID

print "title: %s year: %s" % (movie['title'], movie['year'])

However I see no way to get the picture or thumbnail of the movie cover. Suggestions?

Hospice answered 9/11, 2008 at 6:39 Comment(0)
S
10

Note:

  • Not every movie has a cover url. (The random ID in your example doesn't.)
  • Make sure you're using an up-to-date version of IMDbPy. (IMDb changes, and IMDbPy with it.)

...

import imdb

access = imdb.IMDb()
movie = access.get_movie(1132626)

print "title: %s year: %s" % (movie['title'], movie['year'])
print "Cover url: %s" % movie['cover url']

If for some reason you can't use the above, you can always use something like BeautifulSoup to get the cover url.

from BeautifulSoup import BeautifulSoup
import imdb

access = imdb.IMDb()
movie = access.get_movie(1132626)

page = urllib2.urlopen(access.get_imdbURL(movie))
soup = BeautifulSoup(page)
cover_div = soup.find(attrs={"class" : "photo"})
cover_url = (photo_div.find('img'))['src']
print "Cover url: %s" % cover_url
Saccharose answered 9/11, 2008 at 9:34 Comment(0)
H
2

Response from the IMDbPy mailing list:

If present, the url is accessible through movie['cover url']. Beware that it could be missing, so you must first test it with something like:
if 'cover url' in movie: ...

After that, you can use the urllib module to fetch the image itself.

To provide a complete example, something like that should do the trick:

import urllib
from imdb import IMDb

ia = IMDb(#yourParameters)
movie = ia.get_movie(#theMovieID)

if 'cover url' in movie:
    urlObj = urllib.urlopen(movie['cover url'])
    imageData = urlObj.read()
    urlObj.close()
    # now you can save imageData in a file (open it in binary mode).

In the same way, a person's headshot is stored in person['headshot'].

Things to be aware of:

  • covers and headshots are available only fetching the data from the web server (via the 'http' or 'mobile' data access systems), and not in the plain text data files ('sql' or 'local').
  • using the images, you must respect the terms of the IMDb's policy; see http://imdbpy.sourceforge.net/docs/DISCLAIMER.txt
  • the images you'll get will vary in size; you can use the python-imaging module to rescale them, if needed.
Hospice answered 9/11, 2008 at 11:1 Comment(1)
'cover url' in movie returns false for me even if its there.Disorganize

© 2022 - 2024 — McMap. All rights reserved.