如何使用IMDbPy获得电影的缩略图?

2024-04-29 21:32:33 发布

您现在位置:Python中文网/ 问答频道 /正文

使用IMDbPy很容易从IMDB站点访问电影:

import imdb

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

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

但是我看不到电影封面的图片或缩略图。建议?


Tags: importidget电影access站点titlerandom
2条回答

注意:

  • 不是每部电影都有封面网址。(示例中的随机ID没有。)
  • 确保使用的是最新版本的IMDbPy。(IMDb改变,IMDbPy也随之改变。)

。。。

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']

如果由于某种原因你不能使用上面的内容,你可以使用BeautifulSoup之类的东西来获取封面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

来自IMDbPy邮件列表的响应:

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.

相关问题 更多 >