How do I catch a specific HTTP error in Python?

PythonHttpUrllib2Urllib

Python Problem Overview


I have

import urllib2
try:
   urllib2.urlopen("some url")
except urllib2.HTTPError:
   <whatever>

but what I end up is catching any kind of HTTP error. I want to catch only if the specified webpage doesn't exist (404?).

Python Solutions


Solution 1 - Python

Python 3

from urllib.error import HTTPError

Python 2

from urllib2 import HTTPError

Just catch HTTPError, handle it, and if it's not Error 404, simply use raise to re-raise the exception.

See the Python tutorial.

Here is a complete example for Python 2:

import urllib2
from urllib2 import HTTPError
try:
   urllib2.urlopen("some url")
except HTTPError as err:
   if err.code == 404:
       <whatever>
   else:
       raise

Solution 2 - Python

For Python 3.x

import urllib.request
from urllib.error import HTTPError
try:
    urllib.request.urlretrieve(url, fullpath)
except urllib.error.HTTPError as err:
    print(err.code)

Solution 3 - Python

Tim's answer seems to me as misleading especially when urllib2 does not return the expected code. For example, this error will be fatal (believe or not - it is not uncommon one when downloading urls): > AttributeError: 'URLError' object has no attribute 'code'

Fast, but maybe not the best solution would be code using nested try/except block:

import urllib2
try:
    urllib2.urlopen("some url")
except urllib2.HTTPError as err:
    try:
        if err.code == 404:
            # Handle the error
        else:
            raise
    except:
        ...

More information to the topic of nested try/except blocks [Are nested try/except blocks in python a good programming practice?](https://stackoverflow.com/questions/17015230/are-nested-try-except-blocks-in-python-a-good-programming-practice "Are nested try/except blocks in python a good programming practice?")

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionArnab Sen GuptaView Question on Stackoverflow
Solution 1 - PythonTim PietzckerView Answer on Stackoverflow
Solution 2 - PythonLazikView Answer on Stackoverflow
Solution 3 - PythonsonavolobView Answer on Stackoverflow