When making HTTP requests in Python, you may occasionally run into 404 errors, indicating that the URL you tried to request does not exist on the server. Here are some tips on handling these "page not found" errors gracefully in your Python code.
Check for 404 Errors
After making a request using the
import requests
response = requests.get("http://example.com/path")
if response.status_code == 404:
# Handle 404 error
print("URL does not exist")
else:
# Use response content
print(response.text)
This checks if the status code in the response is 404 before processing the content.
Log and Notify
Instead of just printing, you may want to log these 404 errors to help debug issues later:
import logging
logger = logging.getLogger(__name__)
logger.warning("Got 404 for URL %s", url)
You can also notify someone through email or Slack that a URL is returning 404.
Use a Try-Except Block
Wrap your request in a try-except block to handle exceptions gracefully:
try:
response = requests.get(url)
response.raise_for_status()
except requests.exceptions.HTTPError as err:
if err.response.status_code == 404:
print("404 Error")
This avoids your program crashing if a URL returns 404.
Handling 404s properly ensures your program does not break unexpectedly. Log and monitor these errors to fix invalid URLs in your code.
Related articles:
- Handling HTTP Status Codes with Python Requests
- Handling Responses with urllib in Python
- Handling Errors with aiohttp ClientResponseError
- httpnotfound aiohttp
- Hands-On Guide to Python Requests Status Codes
- Handling HTTP Response Codes with Python's urllib
- Troubleshooting Python Requests Get When Webpage Isn't Loading
Browse by tags:
Browse by language:
Popular articles:
- Web Scraping in Python - The Complete Guide
- Working with Query Parameters in Python Requests
- How to Authenticate with Bearer Tokens in Python Requests
- Building a Simple Proxy Rotator with Kotlin and Jsoup
- The Complete BeautifulSoup Cheatsheet with Examples
- The Complete Playwright Cheatsheet
- Web Scraping using ChatGPT - Complete Guide with Examples