不过对于找不到相应的网页,又要做另外的一番处理:
# 打印下载信息
def reporthook(blocks_read, block_size, total_size):
if not blocks_read:
print ('Connection opened')
if total_size < 0:
print ('Read %d blocks' % blocks_read)
else:
# 如果找不到,页面不存在,可能totalsize就是0,不能计算百分比
print('downloading:%d MB, totalsize:%d MB' % (blocks_read*block_size/1048576.0,total_size/1048576.0))
def Download(path,url):
#url = 'http://downloads.sourceforge.net/sourceforge/alliancep2p/Alliance-v1.0.6.jar'
#filename = url.rsplit("/")[-1]
try:
# python自带的下载函数
urllib.request.urlretrieve(url, path, reporthook)
except IOError as e: # 如果找不到,好像会引发IOError。
print("download ",url,"/nerror:",e)
print("Done:%s/nCopy to:%s" %(url,path))
如果大家还是遇到了问题...请再评论里注上其他解决方案。










