IMDBpy-导演姓名后跟所有分隔的字符

时间:2018-10-25 15:07:52

标签: movie imdbpy

我正在尝试从IMDB中获取有关电影的一些详细信息。

为此,我使用带有以下代码的IMDBpy:

import imdb
ia = imdb.IMDb()
top250 = ia.get_top250_movies()
i = 0;
for topmovie in top250:
    # First, retrieve the movie object using its ID
    movie = ia.get_movie(topmovie.movieID)
    cast = movie.get('cast')
    topActors = 3
    i = i+1;
    actor_names = [actor['name'] for actor in cast[:topActors]]
    #director_name = [director['director'] for director in cast[:topActors]]
    if i <= 10:
          print(movie,  ';', ' | '.join(movie['genres']),
                        ';', ' | '.join(actor_names),
                        ';', ' | '.join(str(movie['director']))
                );
    else:
         break;

但是,当我运行代码时,我会得到以下格式的结果:

The Shawshank Redemption ; Drama ; Tim Robbins | Morgan Freeman | Bob Gunton ; [ | < | P | e | r | s | o | n |   | i | d | : | 0 | 0 | 0 | 1 | 1 | 0 | 4 | [ | h | t | t | p | ] |   | n | a | m | e | : | _ | D | a | r | a | b | o | n | t | , |   | F | r | a | n | k | _ | > | ]
The Godfather ; Crime | Drama ; Marlon Brando | Al Pacino | James Caan ; [ | < | P | e | r | s | o | n |   | i | d | : | 0 | 0 | 0 | 0 | 3 | 3 | 8 | [ | h | t | t | p | ] |   | n | a | m | e | : | _ | C | o | p | p | o | l | a | , |   | F | r | a | n | c | i | s |   | F | o | r | d | _ | > | ]
The Godfather: Part II ; Crime | Drama ; Al Pacino | Robert Duvall | Diane Keaton ; [ | < | P | e | r | s | o | n |   | i | d | : | 0 | 0 | 0 | 0 | 3 | 3 | 8 | [ | h | t | t | p | ] |   | n | a | m | e | : | _ | C | o | p | p | o | l | a | , |   | F | r | a | n | c | i | s |   | F | o | r | d | _ | > | ]
The Dark Knight ; Action | Crime | Drama | Thriller ; Christian Bale | Heath Ledger | Aaron Eckhart ; [ | < | P | e | r | s | o | n |   | i | d | : | 0 | 6 | 3 | 4 | 2 | 4 | 0 | [ | h | t | t | p | ] |   | n | a | m | e | : | _ | N | o | l | a | n | , |   | C | h | r | i | s | t | o | p | h | e | r | _ | > | ]
12 Angry Men ; Crime | Drama ; Martin Balsam | John Fiedler | Lee J. Cobb ; [ | < | P | e | r | s | o | n |   | i | d | : | 0 | 0 | 0 | 1 | 4 | 8 | 6 | [ | h | t | t | p | ] |   | n | a | m | e | : | _ | L | u | m | e | t | , |   | S | i | d | n | e | y | _ | > | ]
Schindler's List ; Biography | Drama | History ; Liam Neeson | Ben Kingsley | Ralph Fiennes ; [ | < | P | e | r | s | o | n |   | i | d | : | 0 | 0 | 0 | 0 | 2 | 2 | 9 | [ | h | t | t | p | ] |   | n | a | m | e | : | _ | S | p | i | e | l | b | e | r | g | , |   | S | t | e | v | e | n | _ | > | ]
The Lord of the Rings: The Return of the King ; Action | Adventure | Drama | Fantasy ; Noel Appleby | Ali Astin | Sean Astin ; [ | < | P | e | r | s | o | n |   | i | d | : | 0 | 0 | 0 | 1 | 3 | 9 | 2 | [ | h | t | t | p | ] |   | n | a | m | e | : | _ | J | a | c | k | s | o | n | , |   | P | e | t | e | r | _ | > | ]
Pulp Fiction ; Crime | Drama ; Tim Roth | Amanda Plummer | Laura Lovelace ; [ | < | P | e | r | s | o | n |   | i | d | : | 0 | 0 | 0 | 0 | 2 | 3 | 3 | [ | h | t | t | p | ] |   | n | a | m | e | : | _ | T | a | r | a | n | t | i | n | o | , |   | Q | u | e | n | t | i | n | _ | > | ]
The Good, the Bad and the Ugly ; Western ; Eli Wallach | Clint Eastwood | Lee Van Cleef ; [ | < | P | e | r | s | o | n |   | i | d | : | 0 | 0 | 0 | 1 | 4 | 6 | 6 | [ | h | t | t | p | ] |   | n | a | m | e | : | _ | L | e | o | n | e | , |   | S | e | r | g | i | o | _ | > | ]
Fight Club ; Drama ; Edward Norton | Brad Pitt | Meat Loaf ; [ | < | P | e | r | s | o | n |   | i | d | : | 0 | 0 | 0 | 0 | 3 | 9 | 9 | [ | h | t | t | p | ] |   | n | a | m | e | : | _ | F | i | n | c | h | e | r | , |   | D | a | v | i | d | _ | > | ]

如您所见,Director的列将返回带倍数的字符...

我该如何解决?

已经解决了这个问题:

import imdb
ia = imdb.IMDb()
top250 = ia.get_top250_movies()
i = 0;
for topmovie in top250:
    # First, retrieve the movie object using its ID
    movie = ia.get_movie(topmovie.movieID)
    cast = movie.get('cast')
    directors = movie.get('director')
    topActors = 3
    i = i+1;
    actor_names = [actor['name'] for actor in cast[:topActors]]
    director_names = [director['name'] for director in directors[:1]]
    if i <= 10:
          print(movie,  '   ;    ', ' | '.join(movie['genres']),
                        '   ;    ', ' | '.join(actor_names),
                        '   ;    ', ' | '.join(director_names)
                );
    else:
         break;

谢谢!

1 个答案:

答案 0 :(得分:0)

movie ['director']是电影对象的列表;将其强制转换为 str ,您将得到类似“ [<< Object1>,]” 的内容,然后将该字符串用作 join < / em>方法。

您应该获得与演员姓名完全相同的导演姓名。

例如: print(movie, ';', ' | '.join(movie['genres']), ';', ' | '.join(actor_names), ';', ' | '.join([d['name'] for d in movie['director']]) );