diff --git a/build.bat b/build.bat deleted file mode 100644 index 2acde5e..0000000 --- a/build.bat +++ /dev/null @@ -1,47 +0,0 @@ -@echo off -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 ( - echo 正在安装 pyinstaller... - pip install pyinstaller -) - -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 - -echo 开始打包... -pyinstaller build.spec --noconfirm - -if exist "dist\guba-indicator\guba-indicator.exe" ( - echo ======================================== - echo 打包成功! - 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 打包失败,请检查错误信息 -) - -pause diff --git a/build.spec b/build.spec index ecaf4fd..b0c8336 100644 --- a/build.spec +++ b/build.spec @@ -5,7 +5,8 @@ import sys from PyInstaller.utils.hooks import collect_data_files, collect_submodules # 添加当前目录到路径 -sys.path.append(os.path.dirname(os.path.abspath(__file__))) +current_dir = os.getcwd() +sys.path.append(current_dir) block_cipher = None diff --git a/config_manager.py b/config_manager.py index ecbe9bc..2852498 100644 --- a/config_manager.py +++ b/config_manager.py @@ -25,7 +25,8 @@ class ConfigManager: "user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36", "fetch_interval": 60, "retry_times": 3, - "retry_interval": 5 + "retry_interval": 5, + "playwright_dir": "" }, "ui": { "opacity": 0.9, @@ -131,7 +132,8 @@ class ConfigManager: def update_spider(self, target_url: str = None, xpath: str = None, user_agent: str = None, fetch_interval: int = None, - retry_times: int = None, retry_interval: int = None): + retry_times: int = None, retry_interval: int = None, + playwright_dir: str = None): """更新爬虫配置""" if target_url: self.config["spider"]["target_url"] = target_url @@ -145,6 +147,8 @@ class ConfigManager: self.config["spider"]["retry_times"] = retry_times if retry_interval: self.config["spider"]["retry_interval"] = retry_interval + if playwright_dir: + self.config["spider"]["playwright_dir"] = playwright_dir logger.info("爬虫配置已更新") self.save_config() diff --git a/spider.py b/spider.py index 3c11cb0..d1b0c85 100644 --- a/spider.py +++ b/spider.py @@ -289,31 +289,48 @@ class SpiderManager: logger.info(f"开始爬取上证所网页截图: {url}") logger.info(f"目标XPath: {xpath}") + # 首先从配置中读取浏览器路径 + config_playwright_dir = self.config.get('playwright_dir') + if config_playwright_dir: + logger.info(f"从配置中获取到Playwright浏览器路径: {config_playwright_dir}") + # 获取当前脚本目录 if getattr(sys, 'frozen', False): # 打包后的环境 current_dir = os.path.dirname(sys.executable) - # 尝试多个可能的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浏览器路径,尝试使用系统默认路径") + # 优先使用配置中的路径 + if config_playwright_dir and os.path.exists(config_playwright_dir): + playwright_dir = config_playwright_dir + logger.info(f"使用配置中指定的Playwright浏览器路径: {playwright_dir}") + else: + # 配置路径不存在或未设置,尝试多个可能的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__)) - playwright_dir = None - logger.info(f"开发环境,当前目录: {current_dir}") + + # 优先使用配置中的路径 + if config_playwright_dir and os.path.exists(config_playwright_dir): + playwright_dir = config_playwright_dir + logger.info(f"使用配置中指定的Playwright浏览器路径: {playwright_dir}") + else: + playwright_dir = None + logger.info(f"开发环境,当前目录: {current_dir}") output_dir = current_dir screenshot_path = os.path.join(output_dir, "sse_screenshot.png") diff --git a/sse_screenshot.png b/sse_screenshot.png new file mode 100644 index 0000000..649cd1e Binary files /dev/null and b/sse_screenshot.png differ