import sys
from pathlib import Path

# 设置一个跨平台的统一路径
def get_playwright_browsers_path():
    # 获取用户主目录

    home_dir = Path.home()

    # 在主目录下创建一个固定的 Playwright 浏览器路径
    playwright_path = home_dir / ".playwright_browsers"

    # 确保路径存在
    playwright_path.mkdir(parents=True, exist_ok=True)

    return str(playwright_path)

# 动态设置浏览器路径
os.environ["PLAYWRIGHT_BROWSERS_PATH"] = get_playwright_browsers_path()

def ensure_browser_installed():
    try:
        from playwright.__main__ import main as playwright_main
        playwright_main()  # 自动安装 Chromium
    except Exception as e:
        print(f"Error during Playwright installation: {e}")
if __name__ == "__main__":
    # check args
    args = sys.argv
    if len(args)>1 and args[1] == 'install':
        ensure_browser_installed()
    app = QApplication(sys.argv)
    main_window = URLFetcherApp()
    main_window.show()
    sys.exit(app.exec_())

打包之后运行

main.exe install

标签: python, playwright, 爬虫

添加新评论