使用urlopen下载进度条

时间:2017-07-02 11:44:48

标签: python url urllib

我的要求是需要我从http网址下载一个包,并查看相同的进度。

我在下面写了代码

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命令来查看下载的进度。对于代码的最小变化,我有任何方法可以获得下载的水平进度详细信息。 非常感谢

2 个答案:

答案 0 :(得分:1)

函数urlretrieve有一个reporthook回调 - 即你传递urlretrieve调用3个参数的函数。

对于进度条,您可以使用任何progress模块和PyPI。例如,请参阅tqdm

答案 1 :(得分:0)

我建议使用现有的软件包,例如 progressbar2

Docs

安装:

pip install progressbar

示例用法:

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