📜  Python IMDbPY - 错误处理

📅  最后修改于: 2022-05-13 01:55:18.594000             🧑  作者: Mango

Python IMDbPY - 错误处理

在本文中,我们将了解如何处理与Python的 IMDb 模块相关的错误,可以通过检查 imdb.IMDbErrorexception 来捕获与 IMDbPY 相关的无效搜索或数据库错误网络问题等错误
为了处理错误,我们必须导入以下内容

from imdb import IMDbError

句法 :

try :

    # code

except IMDbError as e:

    # action to handle it

如果发生任何与 IMDb 相关的错误,那么它将被 except 块捕获。
下面是实现。

Python3
# importing libraries
from imdb import IMDb, IMDbError
 
# try block
try:
     
    # creating instance of imdb
    ia = IMDb()
     
    # getting person (it accept people id only)
    people = ia.get_person('abcd')
     
# except block  
except IMDbError as e:
     
    # printing the exception
    print(e)


Python3
# importing libraries
from imdb import IMDb, IMDbError
 
# try block
try:
     
    # creating instance of imdb
    ia = IMDb()
     
    # searching person
    people = ia.search_person('abcd')
     
# except block  
except IMDbError as e:
     
    # printing the exception
    print(e)


输出 :

invalid personID "abcd": invalid literal for int() with base 10: 'abcd'

另一个例子:在这个我们关闭了互联网连接

Python3

# importing libraries
from imdb import IMDb, IMDbError
 
# try block
try:
     
    # creating instance of imdb
    ia = IMDb()
     
    # searching person
    people = ia.search_person('abcd')
     
# except block  
except IMDbError as e:
     
    # printing the exception
    print(e)

输出 :

{'errcode': None, 'errmsg': 'None', 'url': 'https://www.imdb.com/find?q=abcd&s=nm', 'proxy': '', 'exception type': 'IOError', 'original exception': URLError(gaierror(11001, 'getaddrinfo failed'))}