我使用以下代码获取特定电影的导演姓名。如何获得导演导演的所有电影的清单。
在这种情况下,我选择了《星际穿越》,得到的导演将是克里斯托弗·诺兰。那么,如何列出他执导的所有电影?

from imdb import IMDb
ia = IMDb()

movie = ia.get_movie('0816692')
print "Name of the movie: ", movie
for i in movie['director']:
    print "Director: ", i
    for k in ia.search_person(str(i))[:1]:
        print k.personID

最佳答案

您可以使用带有导演ID的get_person方法作为参数,并通过键director movie访问人员对象来查找他执导的电影的名称:

from imdb import IMDb
ia = IMDb()

movie = ia.get_movie('0816692')
print "Name of the movie: ", movie
for i in movie['director']:
    print "Director: ", i
    for k in ia.search_person(str(i))[:1]:
        director = ia.get_person(k.personID)
        print "Movies directed by %s" % director
        print "-------------------------------"
        for movie_name in director['director movie']:
            print movie_name


更新资料



我对库进行了一些研究,发现没有嵌套的for循环,甚至可以更简单地解决您的问题:

from imdb import IMDb
ia = IMDb()

movie = ia.get_movie('0816692')
print "Name of the movie: ", movie
for i in movie['director']:
    print "Director: ", i
    director = ia.search_person(i["name"])[0]
    ia.update(director)
    print "Movies directed by %s:" % director
    for movie_name in director["director movie"]:
        print movie_name

关于python - 获取导演导演的电影列表-IMDbPY,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29642595/

10-12 23:30