0

此脚本从网站下载文件,并且在大文件中存在问题,因为丢失的数据包导致停止下载......这是代码:

def download(self):

    adres = r"http://example.com/100_MbFile.zip"
    local = adres.split('/')[-1].split('#')[0].split('?')[0]

    try:
        print "Przygotowanie do zapisania pliku " + local
        u = urllib2.urlopen(adres)
        f = open(local, 'wb')
        meta = u.info()
        file_size = int(meta.getheaders("Content-Length")[0])
        print("Downloading: {0} Bytes: {1}".format(adres, file_size))

        file_size_dl = 0
        block_sz = 8192
        while True:
            buffer = u.read(block_sz)
            if not buffer:
                break

            file_size_dl += len(buffer)
            f.write(buffer)
            p = float(file_size_dl) / file_size
            status = r"{0}  [{1:.2%}]".format(file_size_dl, p)
            status = status + chr(8)*(len(status)+1)
            sys.stdout.write(status)


        if file_size_dl == file_size:
            f.close()

任何想法如何下载大文件?

4

1 回答 1

0

要在 Python 2 中下载和保存文件,您有几个选择...

你可以使用urllibhttp ://docs.python.org/2/library/urllib.html#urllib.urlretrieve

这基本上做了你正在尝试的事情:

import urllib

filename = '100_MbFile.zip'
url = 'http://example.com/' + filename

urllib.urlretrieve(url, filename)

...或者您可以使用urllib2, 并指定要读取的块大小(例如在您的示例代码中)。

import urllib2

filename = '100_MbFile.zip'
url = 'http://example.com/' + filename

req = urllib2.urlopen(url)
block_sz = 8192
with open(filename, 'wb') as f:
    while True:
        chunk = req.read(block_sz)
        if not chunk:
            break
        f.write(chunk)
于 2013-04-23T13:19:38.330 回答