Python 官方文档:入门教程 => 点击学习
一个在没有wget工具情况下,写的下载工具,仅供参考!#!/usr/bin/env python#wget programimport sys,urllib,Httplib,urlparsedef reporthook(*progress_
一个在没有wget工具情况下,写的下载工具,仅供参考!
#!/usr/bin/env python
#wget program
import sys,urllib,Httplib,urlparse
def reporthook(*progress_bar_info):
show_progress_bar_inf=progress_bar_info
block_numbers=show_progress_bar_inf[0]
block_size=show_progress_bar_inf[1]
file_total_size=show_progress_bar_inf[2]
temp_file_total_size=block_numbers*block_size
if temp_file_total_size>file_total_size:
print "Download Successful!"
else:
print str(float(temp_file_total_size)/file_total_size*100)[0:5]+"%"
def check_file_exists(url):
host,path=urlparse.urlsplit(url)[1:3]
if ':' in host:
host,port=host.split(':',1)
try:
port=int(port)
except ValueError:
print 'invalid port number %r' %(port,)
sys.exit(1)
else:
port=80
connection=httplib.HTTPConnection(host,port)
connection.request("HEAD",path)
resp=connection.getresponse()
return resp.status
if __name__=='__main__':
for url in sys.argv[1:]:
status=check_file_exists(url)
i=url.rfind('/')
file=url[i+1:]
if status==404:
print file,"not exist!"
sys.exit(1)
print url,"->",file
urllib.urlretrieve(url,file,reporthook)
如果想了解更多,请关注我们的公众号
公众号ID:opdevos
扫码关注
--结束END--
本文标题: python wget
本文链接: https://www.lsjlt.com/news/191958.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0