我正在尝试提取前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没有内容类型...
有人知道如何识别每部电影的每种流派吗?
非常感谢!
答案 0 :(得分:2)
来自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
答案 1 :(得分:1)
这是一个较短的Python代码,可以访问here。
Python提供了一种更简洁的方式来理解我们的代码。在此脚本中,我使用了两种这样的技术。
列表理解只不过是遍历可迭代对象并产生一个列表作为输出。在这里,我们还可以包括计算和条件。与此非常相似的另一种技术,即技术2:词典理解,您可以here对其进行阅读。
例如没有列表理解的代码
const darkTheme = {
main: palette.black,
background: palette.dark_grey,
alternative: palette.white_grey,
trackCardGradient: palette.black,
reviewCardGradient: palette.white_grey,
reviewCardTitle: palette.white_grey,
placeholderColor: palette.grey,
main_font: palette.light_grey,
second_font: palette.light_grey,
empty_star_color: palette.white_grey,
copy_right: palette.white,
search_bar: searchBarDarkTheme,
status_bar: statusBarDarkTheme,
};
const baseTheme = {
main: palette.blue,
background: palette.light_blue,
alternative: palette.white_blue,
trackCardGradient: palette.gradient_blue,
reviewCardGradient: palette.white_blue,
reviewCardTitle: palette.dark_blue,
placeholderColor: palette.midd_blue,
main_font: palette.light_blue,
second_font: palette.blue,
empty_star_color: palette.blue,
copy_right: palette.black,
search_bar: searchBarBaseTheme,
status_bar: statusBarBaseTheme,
};
// export const colors = darkTheme;
export const colors = baseTheme;
使用列表理解的代码
numbers = []
for i in range(10):
numbers.append(i)
print(numbers)
#Output:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
出现OP问题,get_top250_movies()函数返回的电影列表很少有细节。它返回的确切参数可以像这样检查。从输出中可以看到,电影详细信息不包含流派和其他详细信息。
numbers = [i for i in range(10)]
print(numbers)
#Output:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
但是,get_movie()函数返回有关电影的更多信息,包括Genres。
我们结合这两个功能来获得前20部电影的流派。首先,我们调用get_top250_movies(),该方法返回详细信息较少的前250首电影的列表(我们只对获取movieID感兴趣)。然后,我们从顶级电影列表中为每个movieID调用get_movie(),这将向我们返回流派。
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)')]
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}