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

18 Replies, 1122 Views

Hey! Yeah, you can totally handle 404 errors in Python without crashing your script. The easiest way is to use a try-except block with `requests.get()`.

Here's a quick snippet:
```python
import requests

urls = ["http://example.com", "http://nonexistent.com"]
for url in urls:
try:
response = requests.get(url)
response.raise_for_status()
except requests.exceptions.HTTPError:
print(f"Skipping 404 for {url}")
continue
```
This way, it just skips the bad links and keeps going. Hope this helps!

Messages In This Thread



Users browsing this thread: 1 Guest(s)