Files
datie-bom/browser_login/do_search2.py

53 lines
1.8 KiB
Python

import sys
import time
import json
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent))
from login import get_page
from config import OUTPUT_DIR
import datetime
page = get_page(port=9222)
target_tab = page.get_tab(page.latest_tab)
print("当前报表 URL:", target_tab.url)
btn = target_tab.ele('#onSearch') or target_tab.ele('text=查询')
if btn:
print("找到按钮", btn.html)
print("点击查询按钮...")
target_tab.listen.start()
try:
btn.click(by_js=True)
except:
try:
target_tab.run_js('arguments[0].click()', btn)
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("没有找到 查询 按钮")