【问题标题】:IMDBpy - Get Genres from the Top 20 moviesIMDBpy - 从前 20 部电影中获取流派
【发布时间】:2020-04-20 14:31:44
【问题描述】:

我正在尝试提取包含前 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 没有内容类型...

有人知道如何识别每部电影的每种类型吗?

非常感谢!

【问题讨论】:

  • "the object top250" 似乎是一个电影对象的 list,因此您需要遍历每个对象并访问其类型。也许查看setcollections.Counter 来存储所见的独特流派。
  • 如果您尝试打印top250,输出是什么?从错误来看,它似乎是一个列表,因此无法以您尝试的方式访问(这将与 dict 一起使用)
  • @rdimaio 我正在尝试获取电影的名称和每种类型
  • @PedroAlves 试试我在答案中发布的代码,让我知道它是否适合你

标签: python imdbpy


【解决方案1】:

来自IMDbPY docs

“可以检索前 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

【讨论】:

  • 谢谢你们!太棒了!
【解决方案2】:

这里有一段较短的 Pythonic 代码,notebook 可以访问here

Python 提供了一些更简洁的方式来理解我们的代码。在这个脚本中,我使用了两种这样的技术。

技术 1:列表理解

列表推导式只不过是循环遍历一个可迭代对象并生成一个列表作为输出。在这里,我们也可以包括计算和条件。另一种技术,即Technique-2:字典理解,与此非常相似,您可以阅读它here

例如没有列表理解的代码

numbers = []
for i in range(10):
  numbers.append(i)
print(numbers)

#Output:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

使用列表理解的代码

numbers = [i for i in range(10)]
print(numbers)

#Output:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

来到 OPs 问题,get_top250_movies() 函数返回一个包含很少细节的电影列表。可以像这样检查它返回的确切参数。从输出中可以看出,电影详细信息不包含流派和其他详细信息。

from imdb import IMDb
ia = IMDb()
top250Movies = ia.get_top250_movies()
top250Movies[0].items()

#output:
[('rating', 9.2),
 ('title', 'The Shawshank Redemption'),
 ('year', 1994),
 ('votes', 2222548),
 ('top 250 rank', 1),
 ('kind', 'movie'),
 ('canonical title', 'Shawshank Redemption, The'),
 ('long imdb title', 'The Shawshank Redemption (1994)'),
 ('long imdb canonical title', 'Shawshank Redemption, The (1994)'),
 ('smart canonical title', 'Shawshank Redemption, The'),
 ('smart long imdb canonical title', 'Shawshank Redemption, The (1994)')]

但是,get_movie() 函数返回更多关于电影的信息,包括Genres

我们结合这两个函数来获得前 20 部电影的类型。首先,我们调用 get_top250_movies(),它返回包含较少详细信息的前 250 部电影的列表(我们只对获取 movieID 感兴趣)。然后我们为顶级电影列表中的每个电影 ID 调用 get_movie(),这将返回我们的流派。

程序:

from imdb import IMDb    

#initialize and get top 250 movies; this list of movies returned only has 
#fewer details and doesn't have genres
ia = IMDb()
top250Movies = ia.get_top250_movies()

#TECHNIQUE-1: List comprehension
#get top 20 Movies this way which returns lot of details including genres
top20Movies = [ia.get_movie(movie.movieID) for movie in top250Movies[:20]]

#TECHNIQUE-2: Dictionary comprehension
#expected output as a dictionary of movie titles: movie genres
{movie['title']:movie['genres'] for movie in top20Movies}

输出:

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

【讨论】:

  • 虽然此代码可能会解决问题,including an explanation 关于如何以及为什么解决问题将真正有助于提高您的帖子质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人。请edit您的答案添加解释并说明适用的限制和假设。
  • 海布莱恩,谢谢你,我会编辑我的答案并尽力用文字表达。我只在 StackOverflow 上消费了很长时间,但这是我第一次登录并发布。希望人们不会觉得我的回答很奇怪。
猜你喜欢
  • 2019-08-25
  • 2019-05-21
  • 2016-06-17
  • 2016-12-27
  • 2022-01-22
  • 2015-06-20
  • 2019-07-24
  • 2021-12-31
  • 1970-01-01
相关资源
最近更新 更多