使用 urlopen 下载的进度条
Progress bar with downloading using urlopen
我的要求需要我从 http url 下载一个包并查看其进度。
我写了下面的代码
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 }'"
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 的进度,只要对代码进行最少的更改,我就可以获得下载的横向进度详细信息。
非常感谢
我建议使用现有的软件包,例如 progressbar2。
安装:
pip 安装进度条
用法示例:
import progressbar
import urllib
# Instantiate the progress bar
bar = progressbar.ProgressBar()
# Example URLS
urls = ["http://www.whosebug.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
函数 urlretrieve 有一个 reporthook
回调——即你传递一个 urlretrieve
使用 3 个参数调用的函数。
关于进度条,你可以使用任何progress module a PyPI. See, for example, tqdm。
我的要求需要我从 http url 下载一个包并查看其进度。
我写了下面的代码
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 }'"
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 的进度,只要对代码进行最少的更改,我就可以获得下载的横向进度详细信息。 非常感谢
我建议使用现有的软件包,例如 progressbar2。
安装:
pip 安装进度条
用法示例:
import progressbar
import urllib
# Instantiate the progress bar
bar = progressbar.ProgressBar()
# Example URLS
urls = ["http://www.whosebug.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
函数 urlretrieve 有一个 reporthook
回调——即你传递一个 urlretrieve
使用 3 个参数调用的函数。
关于进度条,你可以使用任何progress module a PyPI. See, for example, tqdm。