1 直接下载
这种方法会把要下载的文件全部读进内存,只适用于小文件
import requests
url = 'http://hgdownload.cse.ucsc.edu/goldenPath/rn6/bigZips/rn6.fa.gz'
resp = requests.get(url)
with open('rn6.fa.gz', 'wb') as out:
out.write(resp.content)
2 分块下载
设置stream=True
,当执行iter_content时才开始下载指定大小,节省内存
import requests
url = 'http://hgdownload.cse.ucsc.edu/goldenPath/rn6/bigZips/rn6.fa.gz'
resp = requests.get(url, stream=True)
with open('rn6.fa.gz', 'wb') as out:
for trunk in resp.iter_content(4096): # 每次下载4096字节的数据
out.write(content)