Python IMDbPY - 从电影细节中检索演员
在本文中,我们将了解如何从电影信息中检索电影中表演的演员姓名,电影 id 是 IMDb 赋予每部电影的唯一 ID。我们可以使用 search_movie 方法按名称搜索电影,但它会给出许多电影,因为它们具有相同的名称,因此通过其 id 检索电影是更好的选择。
为了通过 id 搜索电影,我们使用get_movie
方法。
Syntax: [to get actors list] cast = movie[‘cast’]
Here movie is the object returned by the get_movie method which act as the dictionary
下面是实现。
# importing the module
import imdb
# creating instance of IMDb
ia = imdb.IMDb()
# ID
code = "1187043"
# getting movie
movie = ia.get_movie(code)
# printing movie object
print(movie)
print("===============")
# getting cast
cast = movie['cast']
# actor name from caste
actor = cast[0]
# printing actor name
print(actor)
输出 :
3 Idiots
===============
Aamir Khan
另一个例子 -
# importing the module
import imdb
# creating instance of IMDb
ia = imdb.IMDb()
# ID
code = "4434004"
# getting movie
movie = ia.get_movie(code)
# printing movie object
print(movie)
print("===============")
# getting cast
cast = movie['cast']
# actor name from caste
actor = cast[0]
# printing actor name
print(actor)
输出 :
Udta Punjab
===============
Shahid Kapoor