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

@@ -3,6 +3,14 @@ echo ========================================
echo 股吧人气指示器 - 打包工具
echo ========================================
REM 检查并安装必要的依赖
echo 检查并安装依赖...
pip install -r requirements.txt
REM 安装Playwright浏览器
echo 安装Playwright浏览器...
python -m playwright install chromium
REM 检查 pyinstaller 是否安装
pip show pyinstaller >nul 2>&1
if errorlevel 1 (
@@ -10,14 +18,28 @@ if errorlevel 1 (
pip install pyinstaller
)
echo 开始打包...
pyinstaller build.spec
REM 清理旧的构建文件
echo 清理旧的构建文件...
if exist "build" rmdir /s /q build
if exist "dist" rmdir /s /q dist
if exist "guba-indicator.spec" del guba-indicator.spec
if exist "dist\guba-indicator.exe" (
echo 开始打包...
pyinstaller build.spec --noconfirm
if exist "dist\guba-indicator\guba-indicator.exe" (
echo ========================================
echo 打包成功!
echo 可执行文件位置: dist\guba-indicator.exe
echo 可执行文件位置: dist\guba-indicator\guba-indicator.exe
echo ========================================
REM 复制必要的资源文件
echo 复制资源文件...
copy guba.ico dist\guba-indicator\ >nul 2>&1
copy indicator.ico dist\guba-indicator\ >nul 2>&1
copy config.json dist\guba-indicator\ >nul 2>&1
echo 资源文件复制完成!
) else (
echo 打包失败,请检查错误信息
)

View File

@@ -1,55 +1,80 @@
# -*- mode: python ; coding: utf-8 -*-
import sys
import os
import sys
from PyInstaller.utils.hooks import collect_data_files, collect_submodules
# 添加当前目录到路径
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
block_cipher = None
# 添加项目路径
project_path = os.path.abspath('.')
if project_path not in sys.path:
sys.path.insert(0, project_path)
# 收集Playwright的数据文件
playwright_data = collect_data_files('playwright')
# 添加额外的隐藏导入
hiddenimports = [
'PySide6.Qt6Compat',
'loguru',
'playwright._impl._driver',
'playwright._impl._api_structures',
'playwright._impl._connection',
'playwright._impl._transport',
]
# 排除PyQt5避免冲突
excludes = ['PyQt5', 'PyQt6']
a = Analysis(
['main.py'],
pathex=['.', project_path],
pathex=[],
binaries=[],
datas=[],
hiddenimports=[],
datas=[
*playwright_data,
('guba.ico', '.'),
('indicator.ico', '.'),
('config.json', '.'),
],
hiddenimports=hiddenimports,
hookspath=[],
hooksconfig={},
runtime_hooks=[],
excludes=['tkinter', 'IPython', 'pytest', 'matplotlib', 'pandas', 'scipy', 'numpy'],
excludes=excludes,
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False,
)
# 添加必要的二进制文件
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)
exe = EXE(
pyz,
a.scripts,
a.binaries,
a.datas,
[],
exclude_binaries=True,
name='guba-indicator',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
console=False,
upx_exclude=[],
runtime_tmpdir=None,
console=False, # 不显示控制台窗口
icon='guba.ico',
disable_windowed_traceback=False,
argv_emulation=False,
target_arch=None,
codesign_identity=None,
entitlements_file=None,
icon='indicator.ico',
)
# 如果需要单文件,使用以下配置
coll = COLLECT(
exe,
a.binaries,
a.zipfiles,
a.datas,
strip=False,
upx=True,

BIN
main.exe

Binary file not shown.

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 ""

Binary file not shown.

Before

Width:  |  Height:  |  Size: 59 KiB