Python IMDbPY - 用于电影的库

2024 年 8 月 29 日 | 阅读 6 分钟

Python 流行原因之一在于它提供了大量可以简化复杂任务的库。Python IMDB 就是其中一个,对电影爱好者可能很有帮助。

Python IMDbPY 库用于检索和管理 IMDB 的数据。如果你不熟悉 IMDB,它是一个关于全球娱乐信息(包括电影、电视节目、家庭视频、电子游戏和在线流媒体内容,包括演员、导演、制作人员和传记)的在线数据库。用户可以评论和评分电影,还可以找到知名评论家的文章。

要使用此库,我们需要使用以下命令将其安装到系统中。

让我们通过以下示例来了解如何获取电影名称。

示例 - 获取标题中包含“Harry”的所有电影

输出

Harry
Harry
Harry
Bosch
Harry Potter and the Sorcerer's Stone
Harry Potter and the Goblet of Fire
Harry Potter and the Deathly Hallows: Part 1
Harry Potter and the Chamber of Secrets
Barry
Mrs Harris Goes to Paris
The Quarry
Marry Me
Harry Wild
Harry Potter and the Prisoner of Azkaban
When Harry Met Sally...
Harry Potter and the Order of the Phoenix
Dirty Harry
Harry Potter and the Deathly Hallows: Part 2
Harry Potter and the Half-Blood Prince
Harrow

让我们看另一个示例 -

示例 -

输出

Pushpa: The Rise - Part 1
Pushpa
Pushpa: The Rule - Part 2
Pushpaka Vimana
Pushpavalli
Pushpa Impossible
Pushpak Vimaan
Agni Pushpam
Gold Rush: Parker's Trail
Pushpanjali
Pushpaka Vimana
Push Part 1
Pushpaka Vimanam
O Pushpa I Hate Tears
Pushpadhanu
Pushpanjali
Panneer Pushpangal
Push! Push!
Hempushpa TVC
Pushpa Ka Plan

从搜索的电影中获取电影 ID

在本节中,我们将学习如何从搜索的电影中查找电影 ID。电影 ID 是分配给每部电影的唯一 ID,因为电影名称可能相同,但 ID 是不同的。我们使用 search_movie() 方法来搜索同名电影。让我们通过以下语法来理解。

语法 -

这里的 movies 是 search_movie 返回的电影列表,movies[0] 指的是列表中的第一个元素。

参数:不接受任何参数。

返回值:返回一个字符串,即电影 ID。

让我们理解以下示例 -

示例 -

输出

Pushpa: The Rise - Part 1 : 9389998
Pushpa : 6925604
Pushpa: The Rule - Part 2 : 16539454
Pushpaka Vimana : 0251355
Pushpavalli : 7675948
Pushpa Impossible : 20872936
Pushpak Vimaan : 8785380
Agni Pushpam : 0280405
Gold Rush: Parker's Trail : 6639066
Pushpanjali : 0268555
Pushpaka Vimana : 5762180
Push Part 1 : 16961116
Pushpaka Vimanam : 14244714
O Pushpa I Hate Tears : 11261918
Pushpadhanu : 0237623
Pushpanjali : 0258061
Panneer Pushpangal : 0279310
Push! Push! : 0145418
Hempushpa TVC : 6035774
Pushpa Ka Plan : 11248374

让我们通过另一个示例来理解 -

示例 - 2

输出

John Wick : 2911666
The Continental : 6486762
John Wick : 4151952
John Wick: Chapter 4 : 10366206
John Wick: Chapter 3 - Parabellum : 6146586
John Wick: Chapter 2 : 4425200
John Wick: Chapter 5 : 12844100
John Wick: Chapter 3 - Parabellum: HBO First Look : 10275370
John Wick Hex : 11056052
Training 'John Wick' : 7161856
John Wick: Don't F*#% with John Wick : 5278630
John Wick 2 : 7043700
John Wick : 20859452
John Wick : 12297370
John Wick : 5457768
John Wick : 6512986
John Wick : 7046010
John Wick (2019) (Podcast Episode)  - Season 1 | Episode 14  - Al Cine con las Amikas: El Podcast : 19402404
John Wick 3: 13764284
John Wick : 7229466

注意 - IMDbPY 模块已重命名为 cinemagoer。用户也可以使用 cinemagoer 代替 IMDbPY。

获取电影的其他信息

让我们打印给定电影的导演、演员和类型的名称。

示例 -

输出

Movie Name: 
The Matrix
Movie Cast:
Keanu Reeves
Laurence Fishburne
Carrie-Anne Moss
Hugo Weaving
Gloria Foster
Joe Pantoliano
Marcus Chong
Julian Arahanga
Matt Doran
Belinda McClory
Anthony Ray Parker
Paul Goddard
Robert Taylor
David Aston
Marc Aden Gray
Ada Nicodemou
Deni Gordon
Rowan Witt
Eleanor Witt
Tamara Brown
Janaya Pender
Adryn White
Natalie Tjen
Bill Young
David O'Connor
Jeremy Ball
Fiona Johnson
Harry Lawrence
Steve Dodd
Luke Quinton
Lawrence Woodward
Michael Butcher
Bernard Ledger
Robert Simper
Chris Pattinson
Nigel Harbach
Mike Duncan
Nash Edgerton
Rana Morrison
Steve Rutherford
Cody Zacharias
Directors:
Lana Wachowski
Lilly Wachowski
Genres:
Action
Sci-Fi

获取排名前 50 的电影

让我们理解以下示例 -

示例 -

输出

1 : The Godfather
2 : The Dark Knight
3 : The Godfather Part II
4 : 12 Angry Men
5 : Schindler's List
6 : The Lord of the Rings: The Return of the King
7 : Pulp Fiction
8 : The Lord of the Rings: The Fellowship of the Ring
9 : The Good, the Bad and the Ugly
10 : Forrest Gump
11 : Fight Club
12 : Inception
13 : The Lord of the Rings: The Two Towers
14 : Star Wars: Episode V - The Empire Strikes Back
15 : The Matrix
16 : Goodfellas
17 : One Flew Over the Cuckoo's Nest
18 : Se7en
19 : Seven Samurai
20 : It's a Wonderful Life
21 : The Silence of the Lambs
22 : City of God
23 : Saving Private Ryan
24 : Life Is Beautiful
25 : The Green Mile
26 : Interstellar
27 : Star Wars
28 : Terminator 2: Judgment Day
29 : Back to the Future
30 : Spirited Away
31 : Psycho
32 : The Pianist
33 : L�on: The Professional
34 : Parasite
35 : The Lion King
36 : Gladiator
37 : American History X
38 : The Usual Suspects
39 : The Departed
40 : The Prestige
41 : Casablanca
42 : Whiplash
43 : The Intouchables
44 : Harakiri
45 : Modern Times
46 : Top Gun: Maverick
47 : Grave of the Fireflies
48 : Once Upon a Time in the West
49 : Rear Window
50 : Alien

同样,我们可以获取排名后 50 的电影。

输出

90 : In the Name of the King: A Dungeon Siege Tale
91 : The Flintstones in Viva Rock Vegas
92 : The Avengers
93 : The Hungover Games
94 : Street Fighter: The Legend of Chun-Li
95 : Stan Helsing
96 : Speed 2: Cruise Control
97 : The Human Centipede 2 (Full Sequence)
98 : Ballistic: Ecks vs. Sever
99 : Teen Wolf Too

处理异常

如果程序抛出错误,可以通过检查 imdb.IMDB 异常来捕获。让我们通过以下示例来理解。

示例 -

结论

本教程涵盖了 IMDbPY(又名 Cinemagoer 库)几乎所有重要的方法。如果您是热衷于电影的人,并且正在处理需要电影相关信息的项目,那么这个库将非常有益。