Python - Download Files With Requests
Last Updated: 0009Z 12FEB20
(Created: 0009Z 12FEB20)
import io
from pathlib import Path
import requests
with requests.get(url, allow_redirects=True) as resp:
# raise requests.exceptions.HTTPError on 4xx and 5xx server errors
resp.raise_for_status()
with Path('/to/output/file').open('wb') as fd_out:
fd_out.write(resp.content)
# To big to fit in memory
with requests.get(url, allow_redirects=True, stream=True) as resp:
resp.raise_for_status()
with Path('/to/output/file').open('wb') as fd_out:
for chunk in resp.iter_content(chunk_size=io.DEFAULT_BUFFER_SIZE):
if chunk:
fd_out.write(chunk)
# Get content type
resp.headers.get('Content-Type')
'text/html'
# Get content size (w/o download)
resp = requests.head(url, allow_redirects=True)
resp.headers.get('Content-Length')
'57042524' # bytes
References: