2017-07-02 158 views
1

我的需求需要我從http url下載一個包並查看其相關進度。使用urlopen下載進度條

我在下面寫

import subprocess 
from urllib import urlopen 


    class MyClass(object): 
    ''' 
    classdocs 
    ''' 

def url_to_download(self): 
    url_for_download = "someurl" 
    file = "somefilename" 
    print "downloading with urllib" 

    response = urlopen(url_for_download) 
    CHUNK = 16 * 1024 
    with open(file, 'wb') as f: 
     while True: 
      chunk = response.read(CHUNK) 
      cmd = "ls -ltrh" + " " +file + " "+ "|"+ "awk '{print $5}'" 
      p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True) 
      (output, err) = p.communicate() 
      print "the download progress is" + " " + str(output) 
      if not chunk: 
       break 
      f.write(chunk) 


    if __name__ == "__main__": 
    download = MyClass() 
     download.number_of_files() 
     download.url_to_download() 

代碼正如你可以看到我已經使用基本的Linux命令,以查看download.Is有以最小的改動任何代碼方式的進展情況,我可以有一個水平進度下載的詳細信息。 非常感謝提前

回答

1

功能urlretrievereporthook回調 - 即你傳遞一個函數是urlretrieve調用與3個參數。

至於進度條,你可以使用任何progress模塊一個PyPI。例如參見tqdm

+0

感謝alot.i使它與tqdm一起工作 –

0

我建議使用現有的包如progressbar2

Docs

安裝:

PIP安裝進度

實例應用:

import progressbar 
import urllib 


# Instantiate the progress bar 
bar = progressbar.ProgressBar() 

# Example URLS 
urls = ["http://www.stackoverflow.com","http://www.example.com"] 

# Iterate through urls and get the html 
for url in bar(urls): 
    response = urllib.urlopen(url) 
    html = response.read() 
    #Do some additional processing here