pythonpython-3.xexceptionimdbpy

How to handle exception in Python?


I'm working on getting details for list of movies from IMDB using Imdbpy library. 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. 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.


Solution

  • 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.

    I think errors got printed because of some commands inside the imdbpy that are printing on the standard error. 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:

    python3 my_imdb.py 2> /dev/null