1 直接下载

这种方法会把要下载的文件全部读进内存,只适用于小文件

  1. import requests
  2. url = 'http://hgdownload.cse.ucsc.edu/goldenPath/rn6/bigZips/rn6.fa.gz'
  3. resp = requests.get(url)
  4. with open('rn6.fa.gz', 'wb') as out:
  5. out.write(resp.content)

2 分块下载

设置stream=True,当执行iter_content时才开始下载指定大小,节省内存

  1. import requests
  2. url = 'http://hgdownload.cse.ucsc.edu/goldenPath/rn6/bigZips/rn6.fa.gz'
  3. resp = requests.get(url, stream=True)
  4. with open('rn6.fa.gz', 'wb') as out:
  5. for trunk in resp.iter_content(4096): # 每次下载4096字节的数据
  6. out.write(content)