IMDBpy - Получить жанры из 20 лучших фильмов - PullRequest
0 голосов
/ 19 сентября 2018

Я пытаюсь извлечь набор данных с 20 лучшими фильмами, жанрами и актерами.Для этого я пытаюсь с помощью следующего кода:

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;

Я получаю следующую ошибку:

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

Я думаю, что объект top250 не имеет жанров контента...

Кто-нибудь знает, как определить каждый жанр каждого фильма?

Большое спасибо!

1 Ответ

0 голосов
/ 19 сентября 2018

Из документов IMDbPY :

"Возможно получить список из 250 лучших и 100 нижних фильмов:"

>>> 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() возвращает список, поэтому вы не можете напрямую получить доступ к жанру фильма.

Вот решение:

# 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)  

Полный рабочий код:

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=", ")

Вывод:

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
...