What's the best way to download a file to disk using Python3? or How can I download a file to disk wi

18 Replies, 704 Views

If you’re dealing with auth or cookies, `requests` is way easier than `urllib`.

```python
import requests

url = "https://example.com/private_file.zip"
cookies = {"session": "123abc"}
headers = {"User-Agent": "Mozilla/5.0"}

r = requests.get(url, cookies=cookies, headers=headers)
with open("file.zip", "wb") as f:
f.write(r.content)
```

Headers/cookies make `requests` worth the extra install.

Messages In This Thread



Users browsing this thread: 1 Guest(s)