解决Python爬虫在爬资源过程中使用urlretrieve函数下载文件不完全且避免下载时长过长陷入死循环,并在下载文件的过程中显示下载进度
本文作者:StubbornHuang
版权声明:本文为站长原创文章,如果转载请注明原文链接!
原文标题:解决Python爬虫在爬资源过程中使用urlretrieve函数下载文件不完全且避免下载时长过长陷入死循环,并在下载文件的过程中显示下载进度
原文链接:https://www.stubbornhuang.com/33/
发布于:2019年10月24日 22:31:00
修改于:2019年11月14日 22:55:01

import urllib
from urllib.request import urlretrieve
#解决urlretrieve下载文件不完全的问题且避免下载时长过长陷入死循环
def auto_down(url,filename):
try:
urlretrieve(url,filename,jindu)
except socket.timeout:
count = 1
while count <= 15:
try:
urlretrieve(url, filename,jindu)
break
except socket.timeout:
err_info = 'Reloading for %d time' % count if count == 1 else 'Reloading for %d times' % count
print(err_info)
count += 1
if count > 15:
print("下载失败")
#urlretrieve()的回调函数,显示当前的下载进度
#a为已经下载的数据块
#b为数据块大小
#c为远程文件的大小
global myper
def jindu(a,b,c):
if not a:
print("连接打开")
if c<0:
print("要下载的文件大小为0")
else:
global myper
per=100*a*b/c
if per>100:
per=100
myper=per
print("当前下载进度为:" + '%.2f%%' % per)
if per==100:
return True
当前分类随机文章推荐
- Python - 字典dict遍历方法总结 阅读37次,点赞0次
- Python - 读取视频为numpy数组以及将numpy数组转换为视频 阅读22次,点赞0次
- Python - glob模块详解以及glob.glob、glob.iglob函数的使用 阅读64次,点赞0次
- Python - argparse命令行参数解析库用法总结 阅读43次,点赞0次
- opencv-python - 读取视频,不改变视频分辨率修改视频帧率 阅读3727次,点赞2次
- Python - 使用Opencv-Python库获取本机摄像头视频并保存为视频文件 阅读1779次,点赞0次
- Python - 使用websockets库构建websocket服务器 阅读1012次,点赞0次
- Python - 深度学习训练过程使用matplotlib.pyplot实时动态显示loss和acc曲线 阅读1184次,点赞0次
- Pytorch - 使用opencv-python解码视频文件并将视频帧转换为Pytorch tensor作为网络模型输入数据 阅读1405次,点赞0次
- 解决Python爬虫在爬资源过程中使用urlretrieve函数下载文件不完全且避免下载时长过长陷入死循环,并在下载文件的过程中显示下载进度 阅读3185次,点赞0次
全站随机文章推荐
- 资源分享 - Geometry for Computer Graphics - Formulae, Examples and Proofs 英文高清PDF下载 阅读728次,点赞0次
- 资源分享 - Real-Time Cameras - A Guide for Game Designers and Developers 英文高清PDF下载 阅读1295次,点赞0次
- 资源分享 - 3D Math Primer for Graphics and Game Development (First Edition) 英文高清PDF下载 阅读1647次,点赞0次
- C++ - queue存储动态指针时正确释放内存 阅读3924次,点赞2次
- Python - 使用flask_sockets库构建websocket服务器 阅读1617次,点赞0次
- WordPress - 增加百度统计代码 阅读2449次,点赞0次
- 资源分享 - Digital Image Processing , Fourth Edition 英文高清PDF下载 阅读1141次,点赞0次
- 资源分享 - Graphics Gems III 英文高清PDF下载 阅读1479次,点赞0次
- 书籍翻译 – Fundamentals of Computer Graphics, Fourth Edition,第5章 Linear Algebra中文翻译 阅读630次,点赞2次
- 资源分享 - Vector Field Processing on Triangle Meshes高清PDF下载 阅读1559次,点赞0次
评论
149