fix(打包): 改进打包脚本和Playwright浏览器路径处理
- 更新build.bat添加依赖检查和清理步骤 - 修改build.spec配置以正确处理Playwright依赖 - 增强SpiderManager中Playwright路径的查找逻辑 - 添加错误处理和调试信息
This commit is contained in:
30
build.bat
30
build.bat
@@ -3,6 +3,14 @@ echo ========================================
|
|||||||
echo 股吧人气指示器 - 打包工具
|
echo 股吧人气指示器 - 打包工具
|
||||||
echo ========================================
|
echo ========================================
|
||||||
|
|
||||||
|
REM 检查并安装必要的依赖
|
||||||
|
echo 检查并安装依赖...
|
||||||
|
pip install -r requirements.txt
|
||||||
|
|
||||||
|
REM 安装Playwright浏览器
|
||||||
|
echo 安装Playwright浏览器...
|
||||||
|
python -m playwright install chromium
|
||||||
|
|
||||||
REM 检查 pyinstaller 是否安装
|
REM 检查 pyinstaller 是否安装
|
||||||
pip show pyinstaller >nul 2>&1
|
pip show pyinstaller >nul 2>&1
|
||||||
if errorlevel 1 (
|
if errorlevel 1 (
|
||||||
@@ -10,14 +18,28 @@ if errorlevel 1 (
|
|||||||
pip install pyinstaller
|
pip install pyinstaller
|
||||||
)
|
)
|
||||||
|
|
||||||
echo 开始打包...
|
REM 清理旧的构建文件
|
||||||
pyinstaller build.spec
|
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 打包成功!
|
echo 打包成功!
|
||||||
echo 可执行文件位置: dist\guba-indicator.exe
|
echo 可执行文件位置: dist\guba-indicator\guba-indicator.exe
|
||||||
echo ========================================
|
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 (
|
) else (
|
||||||
echo 打包失败,请检查错误信息
|
echo 打包失败,请检查错误信息
|
||||||
)
|
)
|
||||||
|
|||||||
51
build.spec
51
build.spec
@@ -1,55 +1,80 @@
|
|||||||
# -*- mode: python ; coding: utf-8 -*-
|
# -*- mode: python ; coding: utf-8 -*-
|
||||||
import sys
|
|
||||||
import os
|
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
|
block_cipher = None
|
||||||
|
|
||||||
# 添加项目路径
|
# 收集Playwright的数据文件
|
||||||
project_path = os.path.abspath('.')
|
playwright_data = collect_data_files('playwright')
|
||||||
if project_path not in sys.path:
|
|
||||||
sys.path.insert(0, project_path)
|
# 添加额外的隐藏导入
|
||||||
|
hiddenimports = [
|
||||||
|
'PySide6.Qt6Compat',
|
||||||
|
'loguru',
|
||||||
|
'playwright._impl._driver',
|
||||||
|
'playwright._impl._api_structures',
|
||||||
|
'playwright._impl._connection',
|
||||||
|
'playwright._impl._transport',
|
||||||
|
]
|
||||||
|
|
||||||
|
# 排除PyQt5,避免冲突
|
||||||
|
excludes = ['PyQt5', 'PyQt6']
|
||||||
|
|
||||||
a = Analysis(
|
a = Analysis(
|
||||||
['main.py'],
|
['main.py'],
|
||||||
pathex=['.', project_path],
|
pathex=[],
|
||||||
binaries=[],
|
binaries=[],
|
||||||
datas=[],
|
datas=[
|
||||||
hiddenimports=[],
|
*playwright_data,
|
||||||
|
('guba.ico', '.'),
|
||||||
|
('indicator.ico', '.'),
|
||||||
|
('config.json', '.'),
|
||||||
|
],
|
||||||
|
hiddenimports=hiddenimports,
|
||||||
hookspath=[],
|
hookspath=[],
|
||||||
hooksconfig={},
|
hooksconfig={},
|
||||||
runtime_hooks=[],
|
runtime_hooks=[],
|
||||||
excludes=['tkinter', 'IPython', 'pytest', 'matplotlib', 'pandas', 'scipy', 'numpy'],
|
excludes=excludes,
|
||||||
win_no_prefer_redirects=False,
|
win_no_prefer_redirects=False,
|
||||||
win_private_assemblies=False,
|
win_private_assemblies=False,
|
||||||
cipher=block_cipher,
|
cipher=block_cipher,
|
||||||
noarchive=False,
|
noarchive=False,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# 添加必要的二进制文件
|
||||||
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)
|
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)
|
||||||
|
|
||||||
exe = EXE(
|
exe = EXE(
|
||||||
pyz,
|
pyz,
|
||||||
a.scripts,
|
a.scripts,
|
||||||
|
a.binaries,
|
||||||
|
a.datas,
|
||||||
[],
|
[],
|
||||||
exclude_binaries=True,
|
|
||||||
name='guba-indicator',
|
name='guba-indicator',
|
||||||
debug=False,
|
debug=False,
|
||||||
bootloader_ignore_signals=False,
|
bootloader_ignore_signals=False,
|
||||||
strip=False,
|
strip=False,
|
||||||
upx=True,
|
upx=True,
|
||||||
console=False,
|
upx_exclude=[],
|
||||||
|
runtime_tmpdir=None,
|
||||||
|
console=False, # 不显示控制台窗口
|
||||||
|
icon='guba.ico',
|
||||||
disable_windowed_traceback=False,
|
disable_windowed_traceback=False,
|
||||||
argv_emulation=False,
|
argv_emulation=False,
|
||||||
target_arch=None,
|
target_arch=None,
|
||||||
codesign_identity=None,
|
codesign_identity=None,
|
||||||
entitlements_file=None,
|
entitlements_file=None,
|
||||||
icon='indicator.ico',
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# 如果需要单文件,使用以下配置
|
||||||
coll = COLLECT(
|
coll = COLLECT(
|
||||||
exe,
|
exe,
|
||||||
a.binaries,
|
a.binaries,
|
||||||
a.zipfiles,
|
|
||||||
a.datas,
|
a.datas,
|
||||||
strip=False,
|
strip=False,
|
||||||
upx=True,
|
upx=True,
|
||||||
|
|||||||
47
spider.py
47
spider.py
@@ -293,9 +293,22 @@ class SpiderManager:
|
|||||||
if getattr(sys, 'frozen', False):
|
if getattr(sys, 'frozen', False):
|
||||||
# 打包后的环境
|
# 打包后的环境
|
||||||
current_dir = os.path.dirname(sys.executable)
|
current_dir = os.path.dirname(sys.executable)
|
||||||
# 设置Playwright浏览器路径
|
# 尝试多个可能的Playwright浏览器路径
|
||||||
playwright_dir = os.path.join(current_dir, '_internal', 'ms-playwright')
|
possible_paths = [
|
||||||
logger.info(f"打包环境,Playwright浏览器路径: {playwright_dir}")
|
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:
|
else:
|
||||||
# 开发环境
|
# 开发环境
|
||||||
current_dir = os.path.dirname(os.path.abspath(__file__))
|
current_dir = os.path.dirname(os.path.abspath(__file__))
|
||||||
@@ -313,8 +326,21 @@ class SpiderManager:
|
|||||||
}
|
}
|
||||||
|
|
||||||
if playwright_dir:
|
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)
|
browser = p.chromium.launch(**browser_launch_options)
|
||||||
page = browser.new_page()
|
page = browser.new_page()
|
||||||
@@ -351,9 +377,20 @@ class SpiderManager:
|
|||||||
browser.close()
|
browser.close()
|
||||||
return ""
|
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:
|
except Exception as e:
|
||||||
logger.error(f"爬取上证所截图失败: {e}")
|
logger.error(f"爬取上证所截图失败: {e}")
|
||||||
logger.exception(e) # 记录详细异常
|
logger.exception(e) # 记录详细异常
|
||||||
|
|
||||||
|
# 记录环境信息用于调试
|
||||||
|
logger.error(f"环境信息 - Frozen: {getattr(sys, 'frozen', False)}")
|
||||||
|
logger.error(f"当前目录: {current_dir}")
|
||||||
|
logger.error(f"Playwright目录: {playwright_dir}")
|
||||||
|
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
|
Before Width: | Height: | Size: 59 KiB |
Reference in New Issue
Block a user