How to Use Python to Curl a URL and Save the Output to a File?

9 Replies, 1790 Views

For error handling in python curl url to file, you can also log the errors instead of just printing them. Here’s a quick example using `logging`:

```python
import logging
import requests

logging.basicConfig(filename="error.log", level=logging.ERROR)
url = "https://example.com"

try:
response = requests.get(url, timeout=5)
response.raise_for_status()
with open("output.txt", "wb") as file:
file.write(response.content)
except requests.exceptions.RequestException as e:
logging.error(f"Failed to fetch {url}: {e}")
```

This way, you can keep track of what went wrong.

Messages In This Thread



Users browsing this thread: 1 Guest(s)