Python: Efficiently Downloading Files with Retry Logic
def click_download_file(self, urn, path):
'点击下载按钮,下载文件'
downloaded_files_success = [] # 下载成功的文件列表
downloaded_files_failed = [] # 下载失败的文件列表
links = self.browser.safe_get_link(By.CSS_SELECTOR, ATTACHMENT_VIEW_WINDOW_ELEMENT)
for link in links:
# 获取文件名 文件大小
file_title, download_time_limit, file_size = self.file_title_data(link, urn)
self.browser.click_with_link(link)
self.download_alert() # 文件下载过大
logger.info(
'\n >> 【urn = {}】文件名{}文件大小{},下载时间{}'.format(urn, file_title, file_size, download_time_limit))
time.sleep(download_time_limit) # 文件下载时间
# 文件下载
status = self.download_file_is_successfully(path, file_title, file_size)
retry_count = 0
while not status and retry_count < 3:
self.browser.click_with_link(link)
self.download_alert() # 重试需要点击文件下载过大
time.sleep(download_time_limit)
status = self.download_file_is_successfully(path, file_title, file_size)
if status:
downloaded_files_success.append(file_title) # 重试成功
logger.info('\n >> 【urn = {}】文件{}重试下载成功'.format(urn, file_title))
else:
logger.info('\n >> 【urn = {}】文件{}重试下载失败'.format(urn, file_title))
downloaded_files_failed.append(file_title) # 下载失败
retry_count += 1
if status:
downloaded_files_success.append(file_title) # 下载成功
else:
downloaded_files_failed.append(file_title) # 下载失败
if downloaded_files_failed:
logger.info('\n >> 【urn = {}】下载失败文件列表:{}'.format(urn, downloaded_files_failed))
raise FileException(message=ErrorMsg.NO_DOWNLOADABLE_FILES, file_list=downloaded_files_failed)
if downloaded_files_success:
logger.info('\n >> 【urn = {}】下载成功文件列表:{}'.format(urn, downloaded_files_success))
new_file_title = self.modify_file(path, urn, downloaded_files_success) # 下载成功修改文件名
return new_file_title
In this optimized version, the download retry logic is encapsulated within a while loop. This approach ensures that the download is retried a maximum of 3 times before ultimately marking the file as a download failure. The code also includes informative logging for successful and failed downloads, enhancing clarity and troubleshooting capabilities.
This refined code promotes efficiency and readability, making it easier to maintain and understand. The strategic use of a while loop streamlines the retry mechanism, reducing redundancy and ensuring a clean, consistent download process.
原文地址: https://www.cveoy.top/t/topic/qhqA 著作权归作者所有。请勿转载和采集!