兰州网站优化seo,百度一下官网搜索引擎,浏览器打开,管理网络的应用软件遇到一个问题#xff0c;点击按钮自动下载文件#xff0c;路径和文件名都不能自定义#xff0c;可以用 playwright 来解决这个问题
from playwright.sync_api import sync_playwright
import os
import time class ExcelDownloader: def __init__(self, download_pat…遇到一个问题点击按钮自动下载文件路径和文件名都不能自定义可以用 playwright 来解决这个问题
from playwright.sync_api import sync_playwright
import os
import time class ExcelDownloader: def __init__(self, download_path: str): self.download_path os.path.abspath(download_path) os.makedirs(self.download_path, exist_okTrue) def download(self, url: str, file_name: str): with sync_playwright() as p: browser p.chromium.launch(headlessFalse) # 设置 headlessTrue 可以隐藏浏览器 context browser.new_context( accept_downloadsTrue, viewport{width: 1920, height: 1080} ) page context.new_page() try: # 访问页面 page.goto(url) # 等待页面加载完成 page.wait_for_load_state(networkidle) # 等待按钮可见 page.wait_for_selector(button[ng-clickexportOrder()], statevisible) # 开始监听下载 with page.expect_download() as download_info: # 点击导出按钮 page.click(button[ng-clickexportOrder()]) # 获取下载对象 download download_info.value # 构建保存路径 save_path os.path.join(self.download_path, file_name) # 如果文件已存在则删除 if os.path.exists(save_path): os.remove(save_path) # 保存文件 download.save_as(save_path) print(f文件已下载到: {save_path}) return save_path except Exception as e: print(f下载失败: {str(e)}) raise finally: context.close() browser.close() # 使用示例
def main(): # 下载配置 config { url: https://example.com/page, download_path: D:/Downloads, file_name: export.xlsx } downloader ExcelDownloader(config[download_path]) try: file_path downloader.download( urlconfig[url], file_nameconfig[file_name] ) print(f下载成功: {file_path}) except Exception as e: print(f下载失败: {str(e)}) if __name__ __main__: main()