Selenium 模拟爬虫下载文件:精准控制下载时间并处理失败情况
el = '#ctl00_ctl00_Detail_Detail_InvoiceDetails_ctl07_GridView1 a[target='AttachmentViewWindow']'
def click_download_files(self, el): # 查找所有可点击下载的文件 download_links = self.driver.find_elements(By.CSS_SELECTOR, el) for link in download_links: download_successful = False retry_count = 0 while not download_successful and retry_count < 3: try: link.click() # 点击下载链接 # 根据文件大小设置下载时间限制 file_size = get_file_size(link.get_attribute('title')) download_time_limit = calculate_download_time_limit(file_size) time.sleep(download_time_limit) # 判断文件是否下载成功 if check_file_downloaded_successfully(link): download_successful = True else: retry_count += 1 except Exception as e: print('Error occurred while downloading file: ', e) retry_count += 1 if not download_successful: print('File download failed after 3 attempts: ', link.get_attribute('title'))
def get_file_size(title): # 从标题中获取文件大小 file_size_str = title.split('(')[1].split(')')[0].strip() file_size = int(file_size_str) return file_size
def calculate_download_time_limit(file_size): # 根据文件大小计算下载时间限制,可以根据具体需求进行调整 download_time_limit = file_size / 1024 # 假设每秒下载1KB return download_time_limit
def check_file_downloaded_successfully(link): # 判断文件是否下载成功,可以根据具体需求进行判断 # 这里假设通过判断文件是否存在来确定下载是否成功 file_path = get_file_path_from_link(link) if os.path.exists(file_path): return True else: return False
def get_file_path_from_link(link): # 从链接中获取文件路径,可以根据具体需求进行解析 href = link.get_attribute('href') file_path = href.split('DownloadAttachment.aspx?')[1].split('&IsParched=False')[0] return file_path
原文地址: https://www.cveoy.top/t/topic/p8n4 著作权归作者所有。请勿转载和采集!