import sys import time from pathlib import Path import datetime import calendar import json sys.path.insert(0, str(Path(__file__).parent)) from login import get_page from config import OUTPUT_DIR page = get_page(port=9222) target_tab = page.get_tab(page.latest_tab) now = datetime.datetime.now() first_day = datetime.date(now.year, now.month, 1).strftime('%Y-%m-%d') last_day = datetime.date(now.year, now.month, calendar.monthrange(now.year, now.month)[1]).strftime('%Y-%m-%d') print(f"Setting dates: {first_day} to {last_day}") start_label = target_tab.ele('text:下单日期(开始)') if start_label: input_ele = start_label.next().ele('tag:input') if input_ele: input_ele.run_js(f"this.value = '{first_day}';") hidden_input = start_label.next().ele('css:input[type="hidden"]') if hidden_input: hidden_input.run_js(f"this.value = '{first_day}';") visible_input = start_label.next().ele('css:input.textbox-text') if visible_input: visible_input.run_js(f"this.value = '{first_day}';") end_label = target_tab.ele('text:下单日期(结束)') if end_label: input_ele = end_label.next().ele('tag:input') if input_ele: input_ele.run_js(f"this.value = '{last_day}';") hidden_input = end_label.next().ele('css:input[type="hidden"]') if hidden_input: hidden_input.run_js(f"this.value = '{last_day}';") visible_input = end_label.next().ele('css:input.textbox-text') if visible_input: visible_input.run_js(f"this.value = '{last_day}';") print("Dates set.") btn = target_tab.ele('#onSearch') or target_tab.ele('text=查询') if btn: target_tab.listen.start() try: btn.click(by_js=True) except: try: btn.run_js('this.click()') except: pass time.sleep(5) packets = target_tab.listen.steps() for p in packets: if p.method == 'POST' and ('api' in p.url.lower() or 'Report' in p.url or 'Data' in p.url or 'Search' in p.url): if p.response and p.response.body: try: body = p.response.body data = body if isinstance(body, (dict, list)) else json.loads(body) if isinstance(data, dict) and 'result' in data: res = data['result'] print("拦截到数据API:", p.url) print("总条数:", res.get('totalCount')) ts = datetime.datetime.now().strftime("%Y%m%d_%H%M%S") path = OUTPUT_DIR / f"report_abnormal_{ts}.json" with open(path, "w", encoding="utf-8") as f: json.dump(data, f, ensure_ascii=False, indent=2) print("数据已保存至:", path) sys.exit(0) except Exception as e: pass print("没有找到匹配的数据") else: print("没有找到查询按钮")