在网上查到使用urllib下载文件显示百分比的方法如下
def report(count, blockSize, totalSize):
percent = int(count*blockSize*100/totalSize)
sys.stdout.write("\r%d%%" % percent + ' complete')
sys.stdout.flush()
sys.stdout.write('\rFetching ' + name + '...\n')
urllib.urlretrieve(getFile, saveFile, reporthook=report)
sys.stdout.write("\rDownload complete, saved as %s" % (fileName) + '\n\n')
sys.stdout.flush()
而用request下载文件只查到
s=requests.session()
down = s.get(download_url)
local =os.path.join(path,filename)
with open(local, "wb") as f:
f.write(down.content)
问题有两个:
1.如何用request的下载功能实现下载文件显示百分比?
2.urllib能否实现request的session功能,目前使用request下载正常,使用urllib哪怕正常登录了获取到文件地址了下载也是失败
request里好像没有progress callback,urllib2/3里我也没看到。
用request的话你可以通过stream把文件下载,同时显示百分比:
urllib2有opener功能,根request的session差不多。