我的要求是需要我从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命令来查看下载的进度。对于代码的最小变化,我有任何方法可以获得下载的水平进度详细信息。 非常感谢
答案 0 :(得分:1)
函数urlretrieve有一个reporthook
回调 - 即你传递urlretrieve
调用3个参数的函数。
答案 1 :(得分:0)
我建议使用现有的软件包,例如 progressbar2 。
安装:
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