2017-07-02 21 views
1

私の要件では、http urlからパッケージをダウンロードし、同じものの進捗状況を確認する必要があります。urlopenを使用してプログレスバーをダウンロードする

は、私はあなたが私はそこdownload.Isコードに最小限の変更でどのような方法の進行状況を確認するために基本的にLinuxのコマンドを使用している見ることができるように私は、水平方向の進捗を持つことができます

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() 

書かれたコードの下に持っていますダウンロードの詳細。 ありがとうございます。

答えて

1

機能urlretrievereporthookコールバックを持っている - あなたは関数を渡す、つまり3つの引数を持つurlretrieve通話こと:プログレスバー

例使用法

ピップをインストール。

進捗バーについては、progressモジュールをPyPIとして使用できます。たとえば、tqdmを参照してください。

+0

ありがとうalot.iはtqdmで動作しました –

0

progressbar2のような既存のパッケージを使用することをお勧めします。

Docs

インストール:

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 
関連する問題