When working with URLs in Python, you may occasionally run into errors trying to open or read from a web resource. The urllib module provides useful exception classes to handle these cases gracefully.
Common errors like HTTP 404 (page not found) or 500 (server error) are raised as
import urllib.request
import urllib.error
try:
response = urllib.request.urlopen("http://bad.url")
except urllib.error.HTTPError as e:
print(f"HTTP error: {e.code} - {e.reason}")
For temporary network errors like the server being unavailable,
import urllib.request
import urllib.error
max_attempts = 3
for retry in range(max_attempts):
try:
response = urllib.request.urlopen("http://flaky.site")
break
except urllib.error.URLError:
if retry < max_attempts-1:
continue
else:
print("Site appears to be down")
break
Pay attention to the error message - sometimes a protocol or SSL issue can manifest as a
To catch any other exceptions besides HTTP and URL errors, use
import urllib.request
import urllib.error
try:
urllib.request.urlopen("http://")
except urllib.error.URLError as e:
print(f"Generic exception: {str(e)}")
Proper exception handling avoids abrupt crashes in your code when web resources misbehave. The