📌  相关文章
📜  Python IMDbPY - 从电影细节中检索演员扮演的角色

📅  最后修改于: 2022-05-13 01:55:08.090000             🧑  作者: Mango

Python IMDbPY - 从电影细节中检索演员扮演的角色

在本文中,我们将了解如何从电影信息中检索给定电影中演员所扮演的角色,电影 id 是 IMDb 赋予每部电影的唯一 id。我们可以使用 search_movie 方法按名称搜索电影,但它会给出许多电影,因为它们具有相同的名称,因此通过其 id 检索电影是更好的选择。并非所有演员信息都必须在那里,他们扮演的角色因电影而异。

为了通过 id 搜索电影,我们使用get_movie方法。为了获取演员列表,我们使用movie['cast'] ,其中movie是电影对象

下面是实现。

# 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[35]
print(actor)
  
# role played 
role = actor.notes
  
print(role)

输出 :

3 Idiots
===============
Vaidyanathan
(as Prof. Vaidyanathan)

另一个例子

# importing the module
import imdb
  
# creating instance of IMDb
ia = imdb.IMDb()
  
# ID
code = "0075860"
  
# getting movie
movie = ia.get_movie(code)
  
# printing movie object
print(movie)
  
print("===============")
  
# getting cast
cast = movie['cast']
  
# actor name from caste
actor = cast[1]
print(actor)
  
# role played 
role = actor.notes
  
print(role)

输出 :

Close Encounters of the Third Kind
===============
François Truffaut
(as Francois Truffaut)