How to get the top 10 movies of a given genre with IMdbpy?

603 views Asked by At

I am using IMdbpy and I want to extract the top ten movies from a given genre.

I have written following code:

code = "0133093"
  
# getting information
series = ia.get_movie(code)
  
# getting gerne of the series
genre = series.data['genres']

Based on the above output I am getting Action and Sci-fi as a genre.

Now how to get top 10 movies of either action or Sci-fi or both genre.

I got this code but this code gives the top 20 movie and genres that differs. I want top 10 movies based on either Action or Sci-fi.

import imdb

ia = imdb.IMDb()
top250 = ia.get_top250_movies()
for movie_count in range(0, 20):
    movie = ia.get_movie(top250[movie_count].movieID)
    print(movie['title'])print(*movie['genres'], sep=", ")
1

There are 1 answers

4
Shili Ho On
import imdb

ia = imdb.IMDb()
code = "0133093"
  
genre  = ia.get_movie(code).data['genres']

top250 = ia.get_top250_movies()


i = 0
top_10_action_scifi = []
 
 
for movie in top250:
    if i < 10 and not set(genre).isdisjoint(set(ia.get_movie(movie.movieID).data['genres'])):
        top_10_action_scifi.append(movie.movieID)
        i = i + 1
        print(movie)