nick34992
2/17/2017 - 12:33 AM

Python Module urllib: error, urlopen

Python Module urllib: error, urlopen

#!/usr/bin/env python3

import urllib.error
from urllib.request import urlopen

try:
    urlopen('http://www.ietf.org/rfc/rfc0.txt')  # URL does not exist
except urllib.error.HTTPError as err:
    print('status', err.code)    # status 404
    print('reason', err.reason)  # reason Not Found
    print('url', err.url)        # url http://www.ietf.org/rfc/rfc0.txt