Download files using requests
library.
Method 1:
import requests
def download_file(url: str)->str:
local_filename = url.split('/')[-1]
# 注意传入参数 stream=True
with requests.get(url, stream=True) as r:
r.raise_for_status()
with open(local_filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=8192):
f.write(chunk)
return local_filename
Method 2:
Use Response.raw
and shutil.copyfileobj
import requests
import shutil
def download_file(url: str)->str:
local_filename = url.split('/')[-1]
with requests.get(url, stream=True) as r:
with open(local_filename, 'wb') as f:
shutil.copyfileobj(r.raw, f)
return local_filename
If need to add decoding,
response.raw.read = functools.partial(response.raw.read, decode_content=True)