文件下载

使用requests库对文件下载,并显示下载进度.

关于进度的显示, 应该是一些文件服务器会返回content-length请求头的,这样才能计算出进度. 如果没有这个消息头的话就无法知道正在下载文件的总大小了.


In [10]:
url = "http://www.meituan.com/api/v1/divisions" # 美团获取获取已开通城市列表

import requests
import os
def download_file(url, local_file):
    # NOTE the stream=True parameter
    r = requests.get(url, stream=True)
    with open(local_file, 'wb') as f:
        for chunk in r.iter_content(chunk_size=1024): 
            if chunk: # filter out keep-alive new chunks
                f.write(chunk)

download_file(url, 'divisions')
print(os.path.getsize('divisions'))


283892

带进度显示文件下载


In [3]:
import requests
import sys

url = 'http://archive.ubuntu.com/ubuntu/dists/xenial-updates/main/installer-amd64/current/images/hwe-netboot/pxelinux.0'

def download_file_with_progress(url, local_file, chunk_size = 1024):
    r = requests.get(url, stream=True)
    total_length = r.headers.get('Content-Length')
    if not total_length:
        with open(local_file, 'wb') as f:
            for chunk in r.iter_content(chunk_size=chunk_size): 
                if chunk: # filter out keep-alive new chunks
                    f.write(chunk)
        return
    
    total_length = int(total_length)
    received_length = 0
    with open(local_file, 'wb') as f:
        for chunk in r.iter_content(chunk_size=chunk_size):
            received_length += len(chunk)
            print('progress:',received_length * 100.0 / total_length)
            sys.stdout.flush()
            if chunk: # filter out keep-alive new chunks
                f.write(chunk)

download_file_with_progress(url, 'pxelinux.0')


('progress:', 2.393194353557072)
('progress:', 4.786388707114144)
('progress:', 7.1795830606712165)
('progress:', 9.572777414228288)
('progress:', 11.96597176778536)
('progress:', 14.359166121342433)
('progress:', 16.752360474899504)
('progress:', 19.145554828456575)
('progress:', 21.53874918201365)
('progress:', 23.93194353557072)
('progress:', 26.32513788912779)
('progress:', 28.718332242684866)
('progress:', 31.111526596241937)
('progress:', 33.50472094979901)
('progress:', 35.89791530335608)
('progress:', 38.29110965691315)
('progress:', 40.68430401047023)
('progress:', 43.0774983640273)
('progress:', 45.47069271758437)
('progress:', 47.86388707114144)
('progress:', 50.25708142469851)
('progress:', 52.65027577825558)
('progress:', 55.043470131812654)
('progress:', 57.43666448536973)
('progress:', 59.8298588389268)
('progress:', 62.223053192483874)
('progress:', 64.61624754604095)
('progress:', 67.00944189959802)
('progress:', 69.4026362531551)
('progress:', 71.79583060671216)
('progress:', 74.18902496026924)
('progress:', 76.5822193138263)
('progress:', 78.97541366738338)
('progress:', 81.36860802094046)
('progress:', 83.76180237449752)
('progress:', 86.1549967280546)
('progress:', 88.54819108161166)
('progress:', 90.94138543516874)
('progress:', 93.3345797887258)
('progress:', 95.72777414228288)
('progress:', 98.12096849583996)
('progress:', 100.0)