How to download files in Python
Answered
6
What is the correct way to download files from URLs in Python?
W
Asked by
web_developer
Platinum
•
285 rep
1 Answer
24
import requests
# Simple download
url = 'https://example.com/file.zip'
response = requests.get(url)
with open('file.zip', 'wb') as f:
f.write(response.content)
# Stream large files
with requests.get(url, stream=True) as r:
r.raise_for_status()
with open('file.zip', 'wb') as f:
for chunk in r.iter_content(chunk_size=8192):
f.write(chunk)
# With progress
from tqdm import tqdm
total_size = int(r.headers.get('content-length', 0))
with tqdm(total=total_size) as pbar:
for data in r.iter_content(chunk_size=1024):
pbar.update(len(data))
L
Bronze
•
311 rep
Your Answer
You need to be logged in to answer questions.
Log In to Answer