這個腳本下載從網站和大型文件的文件中,有一個問題,因爲丟失的數據包導致停止下載...這裏是代碼:Python的urllib2的未完成的下載文件
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()
任何想法如何下載大文件?
你檢查出這個線程? http://stackoverflow.com/questions/1979435/a-multi-part-threaded-downloader-via-python – 2013-04-23 13:51:08