首页 > 解决方案 > 使用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

在此处输入图像描述

标签: pythonwget

解决方案


似乎没有重新启动下载的功能。我查看了这个包的许多示例 -> 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

推荐阅读