import json, subprocess, sys
AB = ["npx", "agent-browser", "--engine", "chrome"]
url = "https://ggzyjy.sc.gov.cn/jyxx/002002/002002001/20260924/8a69c5eda0ce9aba01a0d114bb8b5e29.html"


def run(args, timeout=120):
    r = subprocess.run(args, capture_output=True, text=True, timeout=timeout)
    return r.stdout + r.stderr


def pj(out):
    s = out.find("{")
    if s < 0:
        return None
    d = 0
    for i in range(s, len(out)):
        if out[i] == "{":
            d += 1
        elif out[i] == "}":
            d -= 1
            if d == 0:
                try:
                    j = json.loads(out[s:i + 1])
                    return j["data"] if isinstance(j.get("data"), dict) else j
                except Exception as e:
                    return {"__parseerror__": str(e), "raw_len": len(out), "frag": out[s:i + 1][:200]}
    return {"__nomatch__": True, "raw_len": len(out)}


o0 = run(AB + ["--session", "dbg02", "--json", "open", url], 120)
print("OPEN OUT:", o0[:300].replace("\n", " "))
import time
time.sleep(14)
o = run(AB + ["--session", "dbg02", "--json", "eval", "document.body.innerText"], 90)
print("RAW LEN:", len(o))
print("RAW HEAD:", o[:200].replace("\n", " "))
j = pj(o)
if j is None:
    print("PARSE: None")
else:
    r = j.get("result")
    print("PARSE keys:", list(j)[:6])
    print("result type:", type(r).__name__, "len:", len(str(r)) if r else 0)
    print("result head:", str(r)[:160].replace("\n", " "))
