我正在使用Imdbpy库从IMDB获取电影列表的详细信息。但是我无法处理生成的异常。

谁能让我知道我要去哪里错了?

这是代码:

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


出现异常时,我什么也不想做。我希望它跳过它。
我什至尝试了except Exception as e。但这没有帮助。它仍然会引发错误。

这是错误:

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'>}


我收到ID = 652的错误消息。
有人请帮我解决这个问题。

最佳答案

实际上,您正在处理异常,您只是决定不做任何处理。

您的代码由于异常处理而继续执行,并且在错误中还有正确的行:

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


如果删除try-catch,则代码将在第一个错误时停止执行。

我认为打印错误是因为imdbpy内部的某些命令正在打印标准错误。如果要避免这些错误,可以通过以下方式启动脚本,例如将标准错误重定向到/dev/null或文件:

python3 my_imdb.py 2> /dev/null

关于python - 如何在Python中处理异常?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55277314/

10-17 01:56