pythonimdbpy

IMDBpy - Get Genres from the Top 20 movies


I'm trying to extracting a dataset with the top 20 movies and each genres and actors. For that I'm trying with the following code:

top250 = ia.get_top250_movies()
limit = 20;
index = 0;
output = []
for item in top250:
    for genre in top250['genres']:
        index += 1;
        if index <= limit:
            print(item['long imdb canonical title'], ": ", genre);
        else:
            break;

I'm getting the following error:

Traceback (most recent call last):
  File "C:/Users/avilares/PycharmProjects/IMDB/IMDB.py", line 21, in <module>
    for genre in top250['genres']:
TypeError: list indices must be integers or slices, not str

I think the object top250 don't have the content genres...

Anyone know how to identify each genre of each movies?

Many thanks!


Solution

  • From the IMDbPY docs:

    "It’s possible to retrieve the list of top 250 and bottom 100 movies:"

    >>> top = ia.get_top250_movies()
    >>> top[0]
    <Movie id:0111161[http] title:_The Shawshank Redemption (1994)_>
    >>> bottom = ia.get_bottom100_movies()
    >>> bottom[0]
    <Movie id:4458206[http] title:_Code Name: K.O.Z. (2015)_>
    

    get_top_250_movies() returns a list, thus you can't access the movie's genre directly.

    Here's a solution:

    # Iterate through the movies in the top 250
    for topmovie in top250:
        # First, retrieve the movie object using its ID
        movie = ia.get_movie(topmovie.movieID)
        # Print the movie's genres
        for genre in movie['genres']:
            print(genre)  
    

    Full working code:

    import imdb
    
    ia = imdb.IMDb()
    top250 = ia.get_top250_movies()
    
    # Iterate through the first 20 movies in the top 250
    for movie_count in range(0, 20):
        # First, retrieve the movie object using its ID
        movie = ia.get_movie(top250[movie_count].movieID)
        # Print movie title and genres
        print(movie['title'])
        print(*movie['genres'], sep=", ")
    

    Output:

    The Shawshank Redemption
    Drama
    The Godfather
    Crime, Drama
    The Godfather: Part II
    Crime, Drama
    The Dark Knight
    Action, Crime, Drama, Thriller
    12 Angry Men
    Crime, Drama
    Schindler's List
    Biography, Drama, History
    The Lord of the Rings: The Return of the King
    Action, Adventure, Drama, Fantasy
    Pulp Fiction
    Crime, Drama
    The Good, the Bad and the Ugly
    Western
    Fight Club
    Drama
    The Lord of the Rings: The Fellowship of the Ring
    Adventure, Drama, Fantasy
    Forrest Gump
    Drama, Romance
    Star Wars: Episode V - The Empire Strikes Back
    Action, Adventure, Fantasy, Sci-Fi
    Inception
    Action, Adventure, Sci-Fi, Thriller
    The Lord of the Rings: The Two Towers
    Adventure, Drama, Fantasy
    One Flew Over the Cuckoo's Nest
    Drama
    Goodfellas
    Crime, Drama
    The Matrix
    Action, Sci-Fi
    Seven Samurai
    Adventure, Drama
    City of God
    Crime, Drama