feat: 添加命令行参数支持以自定义报表日期范围

修改 ReportAutomation 类的构造函数,使其接受可选的 start_date 和 end_date 参数。添加 argparse 模块来解析命令行参数,允许用户指定查询的开始和结束日期。更新日期输入逻辑以支持填充日期范围,而不仅仅是单个目标日期。
This commit is contained in:
2026-04-18 15:44:36 +08:00
parent 556fec602a
commit f87d005232
+37 -19
View File
@@ -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
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())