简体   繁体   English

如何处理 Python 中的异常?

[英]How to handle exception in Python?

I'm working on getting details for list of movies from IMDB using Imdbpy library.我正在使用 Imdbpy 库从 IMDB 获取电影列表的详细信息。 However I'm unable to handle the generated exception.但是我无法处理生成的异常。

Where am I going wrong?我哪里错了?

Here's the code:这是代码:

from imdb import IMDb
ia = IMDb()
from_id = 650
to_id = from_id +5
for  i in range(from_id,to_id):
    try:
        movie_id = ia.get_movie(i)
        title = movie_id['title']
        release_year  = movie_id['year']
        kind = movie_id['kind']
        movieid = movie_id.movieID
        print ("here", title,kind,movieid)
    except:
        pass

I want nothing to be done when an exception arises.我不希望出现异常时什么都不做。 I want it to just skip it.我希望它只是跳过它。 I even tried except Exception as e .我什至试过except Exception as e That wasn't helpful though.但这并没有帮助。 It is still throwing error.它仍然抛出错误。

Here's the error:这是错误:

2019-03-21 15:00:24,230 CRITICAL [imdbpy] C:\Python36x64\lib\site-packages\imdb\_exceptions.py:34: IMDbDataAccessError exception raised; args: ({'errcode': None, 'errmsg': 'None', 'url': 'http://www.imdb.com/title/tt0000652/reference', 'proxy': '', 'exception type': 'IOError', 'original exception': <HTTPError 404: 'Not Found'>},); kwds: {}
Traceback (most recent call last):
  File "C:\Python36x64\lib\site-packages\imdb\parser\http\__init__.py", line 231, in retrieve_unicode
    response = uopener.open(url)
  File "C:\Python36x64\lib\urllib\request.py", line 532, in open
    response = meth(req, response)
  File "C:\Python36x64\lib\urllib\request.py", line 642, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Python36x64\lib\urllib\request.py", line 564, in error
    result = self._call_chain(*args)
  File "C:\Python36x64\lib\urllib\request.py", line 504, in _call_chain
    result = func(*args)
  File "C:\Python36x64\lib\urllib\request.py", line 756, in http_error_302
    return self.parent.open(new, timeout=req.timeout)
  File "C:\Python36x64\lib\urllib\request.py", line 532, in open
    response = meth(req, response)
  File "C:\Python36x64\lib\urllib\request.py", line 642, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Python36x64\lib\urllib\request.py", line 570, in error
    return self._call_chain(*args)
  File "C:\Python36x64\lib\urllib\request.py", line 504, in _call_chain
    result = func(*args)
  File "C:\Python36x64\lib\urllib\request.py", line 650, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 404: Not Found
2019-03-21 15:00:24,237 CRITICAL [imdbpy] C:\Python36x64\lib\site-packages\imdb\__init__.py:714: caught an exception retrieving or parsing "main" info set for mopID "0000652" (accessSystem: http)
Traceback (most recent call last):
  File "C:\Python36x64\lib\site-packages\imdb\parser\http\__init__.py", line 231, in retrieve_unicode
    response = uopener.open(url)
  File "C:\Python36x64\lib\urllib\request.py", line 532, in open
    response = meth(req, response)
  File "C:\Python36x64\lib\urllib\request.py", line 642, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Python36x64\lib\urllib\request.py", line 564, in error
    result = self._call_chain(*args)
  File "C:\Python36x64\lib\urllib\request.py", line 504, in _call_chain
    result = func(*args)
  File "C:\Python36x64\lib\urllib\request.py", line 756, in http_error_302
    return self.parent.open(new, timeout=req.timeout)
  File "C:\Python36x64\lib\urllib\request.py", line 532, in open
    response = meth(req, response)
  File "C:\Python36x64\lib\urllib\request.py", line 642, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Python36x64\lib\urllib\request.py", line 570, in error
    return self._call_chain(*args)
  File "C:\Python36x64\lib\urllib\request.py", line 504, in _call_chain
    result = func(*args)
  File "C:\Python36x64\lib\urllib\request.py", line 650, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 404: Not Found

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Python36x64\lib\site-packages\imdb\__init__.py", line 709, in update
    ret = method(mopID)
  File "C:\Python36x64\lib\site-packages\imdb\parser\http\__init__.py", line 446, in get_movie_main
    cont = self._retrieve(self.urls['movie_main'] % movieID + 'reference')
  File "C:\Python36x64\lib\site-packages\imdb\parser\http\__init__.py", line 406, in _retrieve
    ret = self.urlOpener.retrieve_unicode(url, size=size)
  File "C:\Python36x64\lib\site-packages\imdb\parser\http\__init__.py", line 265, in retrieve_unicode
    'original exception': e}
imdb._exceptions.IMDbDataAccessError: {'errcode': None, 'errmsg': 'None', 'url': 'http://www.imdb.com/title/tt0000652/reference', 'proxy': '', 'exception type': 'IOError', 'original exception': <HTTPError 404: 'Not Found'>}
2019-03-21 15:00:25,253 CRITICAL [imdbpy] C:\Python36x64\lib\site-packages\imdb\_exceptions.py:34: IMDbDataAccessError exception raised; args: ({'errcode': None, 'errmsg': 'None', 'url': 'http://www.imdb.com/title/tt0000652/plotsummary', 'proxy': '', 'exception type': 'IOError', 'original exception': <HTTPError 404: 'Not Found'>},); kwds: {}
Traceback (most recent call last):
  File "C:\Python36x64\lib\site-packages\imdb\parser\http\__init__.py", line 231, in retrieve_unicode
    response = uopener.open(url)
  File "C:\Python36x64\lib\urllib\request.py", line 532, in open
    response = meth(req, response)
  File "C:\Python36x64\lib\urllib\request.py", line 642, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Python36x64\lib\urllib\request.py", line 564, in error
    result = self._call_chain(*args)
  File "C:\Python36x64\lib\urllib\request.py", line 504, in _call_chain
    result = func(*args)
  File "C:\Python36x64\lib\urllib\request.py", line 756, in http_error_302
    return self.parent.open(new, timeout=req.timeout)
  File "C:\Python36x64\lib\urllib\request.py", line 532, in open
    response = meth(req, response)
  File "C:\Python36x64\lib\urllib\request.py", line 642, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Python36x64\lib\urllib\request.py", line 570, in error
    return self._call_chain(*args)
  File "C:\Python36x64\lib\urllib\request.py", line 504, in _call_chain
    result = func(*args)
  File "C:\Python36x64\lib\urllib\request.py", line 650, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 404: Not Found
2019-03-21 15:00:25,254 CRITICAL [imdbpy] C:\Python36x64\lib\site-packages\imdb\__init__.py:714: caught an exception retrieving or parsing "plot" info set for mopID "0000652" (accessSystem: http)
Traceback (most recent call last):
  File "C:\Python36x64\lib\site-packages\imdb\parser\http\__init__.py", line 231, in retrieve_unicode
    response = uopener.open(url)
  File "C:\Python36x64\lib\urllib\request.py", line 532, in open
    response = meth(req, response)
  File "C:\Python36x64\lib\urllib\request.py", line 642, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Python36x64\lib\urllib\request.py", line 564, in error
    result = self._call_chain(*args)
  File "C:\Python36x64\lib\urllib\request.py", line 504, in _call_chain
    result = func(*args)
  File "C:\Python36x64\lib\urllib\request.py", line 756, in http_error_302
    return self.parent.open(new, timeout=req.timeout)
  File "C:\Python36x64\lib\urllib\request.py", line 532, in open
    response = meth(req, response)
  File "C:\Python36x64\lib\urllib\request.py", line 642, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Python36x64\lib\urllib\request.py", line 570, in error
    return self._call_chain(*args)
  File "C:\Python36x64\lib\urllib\request.py", line 504, in _call_chain
    result = func(*args)
  File "C:\Python36x64\lib\urllib\request.py", line 650, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 404: Not Found

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Python36x64\lib\site-packages\imdb\__init__.py", line 709, in update
    ret = method(mopID)
  File "C:\Python36x64\lib\site-packages\imdb\parser\http\__init__.py", line 454, in get_movie_plot
    cont = self._retrieve(self.urls['movie_main'] % movieID + 'plotsummary')
  File "C:\Python36x64\lib\site-packages\imdb\parser\http\__init__.py", line 406, in _retrieve
    ret = self.urlOpener.retrieve_unicode(url, size=size)
  File "C:\Python36x64\lib\site-packages\imdb\parser\http\__init__.py", line 265, in retrieve_unicode
    'original exception': e}
imdb._exceptions.IMDbDataAccessError: {'errcode': None, 'errmsg': 'None', 'url': 'http://www.imdb.com/title/tt0000652/plotsummary', 'proxy': '', 'exception type': 'IOError', 'original exception': <HTTPError 404: 'Not Found'>}

I'm getting error for id = 652 .我收到id = 652 的错误。

Actually you are handling the exception, you just decided to handling it without doing anything.实际上,您正在处理异常,您只是决定不做任何事情就处理它。

Your code thanks to the exception handling continues executing and among the errors there are also the correct lines:由于异常处理,您的代码继续执行,并且在错误中还有正确的行:

here The Boston Tea Party movie 0000650
here The Boy Detective, or The Abductors Foiled movie 0000651
here A Calamitous Elopement movie 0000653
here The Call of the Wild movie 0000654

If you remove the try-catch your code will stop executing at the first error.如果您删除 try-catch,您的代码将在第一个错误处停止执行。

I think errors got printed because of some commands inside the imdbpy that are printing on the standard error.我认为错误是因为 imdbpy 中的一些命令在标准错误上打印而被打印出来的。 If you want to avoid them you can redirect the standard error to /dev/null or a file for example by launching your script in this way:如果您想避免它们,您可以将标准错误重定向到/dev/null或文件,例如通过以这种方式启动脚本:

python3 my_imdb.py 2> /dev/null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM