How to use Python to get URL and ignore 404 errors?

18 Replies, 1412 Views

Hey! If you’re using `requests` and want to avoid exceptions, you can set `stream=True` and check the status code before reading the content. This way, you don’t waste time downloading the body for a 404.

Example:
```python
import requests

url = "http://example.com/404"
response = requests.get(url, stream=True)
if response.status_code == 404:
print("404, skipping!")
else:
print(response.content)
```
This is a neat trick for python get url ignore 404 scenarios.

Messages In This Thread



Users browsing this thread: 1 Guest(s)