2013-04-23 64 views
0

這個腳本下載從網站和大型文件的文件中,有一個問題,因爲丟失的數據包導致停止下載...這裏是代碼: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() 

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

+0

你檢查出這個線程? http://stackoverflow.com/questions/1979435/a-multi-part-threaded-downloader-via-python – 2013-04-23 13:51:08

回答

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) 
+0

這種解決方案不工作,因爲檢索它是不完整的...我已經尋找大文件的解決方案。 – Cieniu 2013-04-23 13:32:40

+0

這應該在任何*大小的文件上工作(甚至比你可以在你的機器上的內存中緩衝大)。如果您沒有獲得完整的內容,您的服務有問題。 – 2013-04-23 14:17:20