diff --git a/build.bat b/build.bat index 938a814..2acde5e 100644 --- a/build.bat +++ b/build.bat @@ -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 打包失败,请检查错误信息 ) diff --git a/build.spec b/build.spec index f4dabed..ecaf4fd 100644 --- a/build.spec +++ b/build.spec @@ -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, diff --git a/main.exe b/main.exe index 29b3e48..4c136cd 100644 Binary files a/main.exe and b/main.exe differ diff --git a/spider.py b/spider.py index f2d8cf2..3c11cb0 100644 --- a/spider.py +++ b/spider.py @@ -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 "" diff --git a/sse_screenshot.png b/sse_screenshot.png deleted file mode 100644 index 124f8db..0000000 Binary files a/sse_screenshot.png and /dev/null differ