Python Requests Exceptions List

Honestly, this should be a default setting for the requests library. Without it, your code is doomed if you get any connection errors. Add a ‘try’ when connecting to your URL, then use these exceptions to let your code keep running if there are any errors. This works great for when you’re scraping many URLs at once.

try:
    r = requests.get(url, timeout=10)

except requests.RequestException:
    print('error request')
except requests.ConnectionError:
    print('error connection')
except requests.HTTPError:
    print('error http')
except requests.TooManyRedirects:
    print('error redirects')
except requests.ConnectTimeout:
    print('error connecttimeout')
except requests.ReadTimeout:
    print('error readtimeout')
except requests.Timeout:
    print('error timeout')

Leave your thoughts below. Follow on Twitter