urllib和request的爬虫的两个小问题

在网上查到使用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哪怕正常登录了获取到文件地址了下载也是失败

阅读 2.9k
1 个回答
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进