python requests如何一边下载一边保存文件
问题
使用python requests, 如何下载较大文件,且一边下载一边保存
解决方法
设定stream=True并使用response.iter_content()
def download_and_save(url, save_to):
r = requests.get(url, stream=True)
with open(save_to, 'wb') as file:
for chunk in r.iter_content(chunk_size=1024): # 可自行调整chunk_size大小,其单位为bytes
file.write(chunk)