fix(打包): 改进打包脚本和Playwright浏览器路径处理

- 更新build.bat添加依赖检查和清理步骤
- 修改build.spec配置以正确处理Playwright依赖
- 增强SpiderManager中Playwright路径的查找逻辑
- 添加错误处理和调试信息
This commit is contained in:
2026-01-20 15:11:03 +08:00
parent f23feaf140
commit 346d4a7c99
5 changed files with 106 additions and 22 deletions

View File

@@ -293,9 +293,22 @@ class SpiderManager:
if getattr(sys, 'frozen', False):
# 打包后的环境
current_dir = os.path.dirname(sys.executable)
# 设置Playwright浏览器路径
playwright_dir = os.path.join(current_dir, '_internal', 'ms-playwright')
logger.info(f"打包环境Playwright浏览器路径: {playwright_dir}")
# 尝试多个可能的Playwright浏览器路径
possible_paths = [
os.path.join(current_dir, '_internal', 'ms-playwright'),
os.path.join(current_dir, 'ms-playwright'),
os.path.join(os.path.dirname(current_dir), 'ms-playwright')
]
playwright_dir = None
for path in possible_paths:
if os.path.exists(path):
playwright_dir = path
logger.info(f"找到Playwright浏览器路径: {playwright_dir}")
break
if not playwright_dir:
logger.warning("未找到Playwright浏览器路径尝试使用系统默认路径")
else:
# 开发环境
current_dir = os.path.dirname(os.path.abspath(__file__))
@@ -313,8 +326,21 @@ class SpiderManager:
}
if playwright_dir:
browser_launch_options['executable_path'] = os.path.join(playwright_dir, 'chromium-1091', 'chrome-win', 'chrome.exe')
logger.info(f"使用自定义浏览器路径: {browser_launch_options['executable_path']}")
# 尝试多个可能的浏览器可执行文件路径
possible_executables = [
os.path.join(playwright_dir, 'chromium-1091', 'chrome-win', 'chrome.exe'),
os.path.join(playwright_dir, 'chromium', 'chrome-win', 'chrome.exe'),
os.path.join(playwright_dir, 'chrome-win', 'chrome.exe')
]
for executable_path in possible_executables:
if os.path.exists(executable_path):
browser_launch_options['executable_path'] = executable_path
logger.info(f"使用自定义浏览器路径: {executable_path}")
break
if 'executable_path' not in browser_launch_options:
logger.warning("未找到浏览器可执行文件,使用系统默认浏览器")
browser = p.chromium.launch(**browser_launch_options)
page = browser.new_page()
@@ -351,9 +377,20 @@ class SpiderManager:
browser.close()
return ""
except ImportError as e:
logger.error(f"Playwright导入失败: {e}")
logger.error("请确保已安装playwright: pip install playwright")
logger.error("并安装浏览器: python -m playwright install chromium")
return ""
except Exception as e:
logger.error(f"爬取上证所截图失败: {e}")
logger.exception(e) # 记录详细异常
# 记录环境信息用于调试
logger.error(f"环境信息 - Frozen: {getattr(sys, 'frozen', False)}")
logger.error(f"当前目录: {current_dir}")
logger.error(f"Playwright目录: {playwright_dir}")
return ""