使用urlopen下载进度条

使用urlopen下载进度条

问题描述:

我的需求需要我从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有以最小的改动任何代码方式的进展情况,我可以有一个水平进度下载的详细信息。 非常感谢提前

功能urlretrievereporthook回调 - 即你传递一个函数是urlretrieve调用与3个参数。

至于进度条,你可以使用任何progress模块一个PyPI。例如参见tqdm

+0

感谢alot.i使它与tqdm一起工作 –

我建议使用现有的包如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