Open In App
Related Articles

Python IMDbPY – Retrieving role played by actor from the movie details

Improve Article
Improve
Save Article
Save
Like Article
Like

In this article we will see how we can retrieve the role played by the actor in a given movie from the movie information, movie id is the unique id given to each movie by IMDb. We can use search_movie method to search the movies by their name but it gives many movies as they have same names therefore retrieving a movie by its id is a better option. It is not compulsory that all actor information will be there and what role they have played it varies from movie to movie. In order to search a movie by its id we use get_movie method. and for getting actors list we use movie[‘cast’] where movie is the movie object

Syntax to get role played by actor : role = cast[n].notes Here cast is the list returned by movie[‘cast]

Below is the implementation. 

Python3




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

Output :

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

Another example 

Python3




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

Output :

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

Last Updated : 27 Jan, 2023
Like Article
Save Article
Similar Reads
Related Tutorials