Python通过requests从一个链接下载存放在临时文件tempfile
- import requests
- import tempfile
-
-
- def download(url):
- r = requests.get(url, stream=True)
- file_len = int(r.headers.get('content-length'))
- tf = tempfile.mktemp(suffix='.zip', dir='./')
-
- f = open(tf, 'wb')
-
- count = 0
- for ch in r:
- count = count + len(ch)
-
- f.write(ch)
-
- progress = ''.format(count / file_len * 100)
- print(f'{count}/{file_len}:{progress}%')
-
- f.close()