python - 使用python Wget lib时下载错误
问题描述
如果下载卡在 XX%,有没有办法重新开始下载?我正在尝试抓取并下载很多文件。我正在使用下面的代码。它将解决连接错误,但如果卡住,它将不会重新启动任何下载。
for element in elements:
for attempt in range(100):
try:
wget.download(element.get_attribute("href"), path)
except:
print("attempt error, retry" + str(attempt))
else:
break
解决方案
似乎没有重新启动下载的功能。我查看了这个包的许多示例 -> https://www.programcreek.com/python/example/83386/wget.download。手册页面已经消失,pypi.org 页面确实包含有关此类功能的任何信息。
但是,您只需将另一行添加到except
. 此代码将为您工作。
# Set some variables to end loop after download success
# The download loop will exit if failed 5 times
downloaded = False
attempts = 0
for element in elements:
while not downloaded and attempts < 5:
try:
wget.download(element.get_attribute("href"), path)
# Set downloaded flag to end loop
downloaded = True
except:
print("attempt error, retry" + str(attempt))
wget.download(element.get_attribute("href"), path)
attempts += 1
推荐阅读
- javascript - Mongo 聚合过滤器 $project 数组与另一个数组
- r - 如何在 R 中的混合效应模型中处理边界(奇异)拟合误差?
- c++ - 如何舍入到数字的下一个 50?
- python-3.x - 如何修复 'C 扩展未加载,训练会很慢。安装 C 编译器并重新安装 gensim 以进行快速训练。
- java - NumberFormatException.for InputString : "("
- c# - 如何检测页面上键入的字符串?
- .htaccess - 试图强制 WWW 和 HTPS,而我的三个规则中只有两个会起作用
- smartsheet-api - 如何列出特定行或多行?
- c++ - 是否可以使用 Windows 映像组件访问 HEIF 的“图像属性”元数据,例如“ispe”和“colr”?
- python - 使用 asyncio 的简单 Python TCP 分叉服务器