From f87d00523299fb66edbfae16ed977fa6bb7597f3 Mon Sep 17 00:00:00 2001 From: houhuan Date: Sat, 18 Apr 2026 15:44:36 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E8=A1=8C=E5=8F=82=E6=95=B0=E6=94=AF=E6=8C=81=E4=BB=A5=E8=87=AA?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E6=8A=A5=E8=A1=A8=E6=97=A5=E6=9C=9F=E8=8C=83?= =?UTF-8?q?=E5=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改 ReportAutomation 类的构造函数,使其接受可选的 start_date 和 end_date 参数。添加 argparse 模块来解析命令行参数,允许用户指定查询的开始和结束日期。更新日期输入逻辑以支持填充日期范围,而不仅仅是单个目标日期。 --- main.py | 58 +++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 20 deletions(-) diff --git a/main.py b/main.py index 6b1ece3..0eb994c 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,8 @@ import asyncio import os import logging +import sys +import argparse from datetime import datetime from playwright.async_api import async_playwright @@ -16,13 +18,20 @@ logging.basicConfig( logger = logging.getLogger(__name__) class ReportAutomation: - def __init__(self): + def __init__(self, start_date=None, end_date=None): self.secsion_login_url = "https://secsion.com:8000/login?redirect=%252Fhomepage" self.secsion_stats_url = "https://secsion.com:8000/commodityStatistics" self.upload_url = "https://sale.94kan.cn/" self.username = "15682076681" self.password = "123456" - self.target_date = "2026-04-18" + + # 使用传入的日期,如果没有则默认为今天 + today = datetime.now().strftime('%Y-%m-%d') + self.start_date = start_date or today + self.end_date = end_date or start_date or today + + logger.info(f"任务初始化: 开始日期={self.start_date}, 结束日期={self.end_date}") + self.download_dir = os.path.join(os.getcwd(), "downloads") if not os.path.exists(self.download_dir): @@ -106,30 +115,33 @@ class ReportAutomation: export_btn = page.get_by_role("button", name="导出报表") await export_btn.wait_for(state="visible", timeout=20000) - logger.info(f"设置查询日期: {self.target_date}") - - # 尝试通过日期值定位输入框,或者直接点击导出(如果日期已正确) - # 根据截图,日期可能已经默认是今天 + logger.info(f"设置查询日期范围: {self.start_date} 至 {self.end_date}") try: - # 尝试定位输入框并填入日期 - # 如果 placeholder 不对,我们尝试寻找所有 input 并根据格式填充 + # 找到所有的日期输入框 inputs = page.locator("input") count = await inputs.count() - logger.info(f"页面共有 {count} 个输入框") - # 寻找包含日期的输入框并填入 + date_input_indices = [] for i in range(count): - try: - val = await inputs.nth(i).input_value() - if "-" in val and len(val) >= 10: # 类似 2026-04-18 - # 降低 fill 的超时,因为如果是 readonly 会很快报错 - await inputs.nth(i).fill(self.target_date, timeout=2000) - await inputs.nth(i).press("Enter") - except Exception: - continue + val = await inputs.nth(i).input_value() + if "-" in val and len(val) >= 10: + date_input_indices.append(i) + + if len(date_input_indices) >= 2: + # 填入开始日期 + await inputs.nth(date_input_indices[0]).click() + await inputs.nth(date_input_indices[0]).fill(self.start_date) + await inputs.nth(date_input_indices[0]).press("Enter") + + # 填入结束日期 + await inputs.nth(date_input_indices[1]).click() + await inputs.nth(date_input_indices[1]).fill(self.end_date) + await inputs.nth(date_input_indices[1]).press("Enter") + else: + logger.warning("未找到足够的日期输入框,尝试使用默认日期导出") except Exception as e: - logger.warning(f"填充日期时遇到问题 (可能已默认正确): {str(e)}") + logger.warning(f"填充日期时遇到问题: {str(e)}") # 点击导出报表并捕获下载 logger.info("点击导出报表...") @@ -179,5 +191,11 @@ class ReportAutomation: await page.screenshot(path="upload_result.png") if __name__ == "__main__": - automation = ReportAutomation() + parser = argparse.ArgumentParser(description='报表自动化导出上传工具') + parser.add_argument('--start', type=str, help='开始日期 (格式: YYYY-MM-DD)') + parser.add_argument('--end', type=str, help='结束日期 (格式: YYYY-MM-DD)') + + args = parser.parse_args() + + automation = ReportAutomation(start_date=args.start, end_date=args.end) asyncio.run(automation.run())