"""七次探测:点卡片后 detail.json 明文字段 / 右侧面板沟通按钮 href / 聊天页结构""" import json import time from pathlib import Path import websocket from client.browser.cdp_lite import LiteCDP from client.browser.manager import BrowserManager OUT = Path(__file__).resolve().parent / "data" / "debug" / "job_page_probe7.txt" _lines: list[str] = [] def log(*a): s = " ".join(str(x) for x in a) _lines.append(s) OUT.write_text("\n".join(_lines), encoding="utf-8") class Session: def __init__(self, ws_url): self.ws = websocket.create_connection(ws_url, timeout=30) self._id = 0 def call(self, method, params=None, timeout=10): self._id += 1 mid = self._id self.ws.send(json.dumps({"id": mid, "method": method, "params": params or {}})) self.ws.settimeout(timeout) while True: msg = json.loads(self.ws.recv()) if msg.get("id") == mid: return msg.get("result") or {} def eval_js(self, expr, timeout=10): return (self.call("Runtime.evaluate", {"expression": expr, "returnByValue": True}, timeout) .get("result", {}).get("value")) def pump(self, duration): self.ws.settimeout(0.5) end = time.time() + duration while time.time() < end: try: yield json.loads(self.ws.recv()) except Exception: continue def close(self): try: self.ws.close() except Exception: pass def main(): BrowserManager().ensure_browser_started() lite = LiteCDP() tab = lite.open_tab("https://www.zhipin.com/web/geek/jobs") s = Session(tab["webSocketDebuggerUrl"]) s.call("Network.enable") s.eval_js("""(() => { const inp = document.querySelector('input[placeholder*="搜索"]'); const setter = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, 'value').set; setter.call(inp, 'java'); inp.dispatchEvent(new Event('input', {bubbles: true})); document.querySelector('.search-btn').click(); return true; })()""") time.sleep(5) # 点击第一张卡片,捕获 detail.json s.eval_js("""(() => { const c = document.querySelector('.job-card-wrap'); if (c) c.click(); return !!c; })()""") req_urls: dict[str, str] = {} detail_bodies: list[str] = [] for msg in s.pump(5): m = msg.get("method") if m == "Network.responseReceived": u = msg["params"]["response"]["url"] if "detail.json" in u: req_urls[msg["params"]["requestId"]] = u elif m == "Network.loadingFinished": rid = msg["params"]["requestId"] if rid in req_urls: detail_bodies.append( s.call("Network.getResponseBody", {"requestId": rid}).get("body", "")) for body in detail_bodies[:1]: try: zp = json.loads(body).get("zpData", {}).get("jobInfo") or json.loads(body).get("zpData", {}) log("== 1) detail.json 字段 ==", json.dumps({ "keys": list(zp.keys())[:40], "salary": zp.get("salaryDesc") or zp.get("salary"), "jdHead": (zp.get("jobDesc") or "")[:100], }, ensure_ascii=False)[:2000]) except Exception as e: log("== 1) 解析失败 ==", e, body[:300]) # 右侧面板:沟通按钮 href + JD 容器 r2 = s.eval_js("""(() => { const chat = document.querySelector('.op-btn-chat'); const deliver = [...document.querySelectorAll('a,button')].find(e => /投递简历/.test(e.innerText || '')); const jd = document.querySelector('.job-sec-text, .job-detail-section'); return JSON.stringify({ chatHref: chat ? chat.getAttribute('href') : null, chatClass: chat ? chat.className : null, deliverHref: deliver ? deliver.getAttribute('href') : null, jdClass: jd ? jd.className : null, jdHead: jd ? jd.innerText.slice(0, 60) : null }); })()""") log("== 2) 右侧面板 ==", r2) s.close() lite.close_tab(tab["id"]) # 聊天页:用 href 原生打开 chat_href = (json.loads(r2) if r2 else {}).get("chatHref") if chat_href: before = {t["id"] for t in lite.list_tabs()} ctab = lite.open_tab(chat_href if chat_href.startswith("http") else "https://www.zhipin.com" + chat_href) c = Session(ctab["webSocketDebuggerUrl"]) time.sleep(5) r3 = c.eval_js("""(() => { const ta = document.querySelector('textarea'); const sends = [...document.querySelectorAll('a,button,div')] .filter(e => (e.innerText || '').trim() === '发送' && e.offsetHeight > 0) .map(e => ({tag: e.tagName, cls: e.className})); const sendBtn = document.querySelector('.chat-input .btn-send, .btn-send'); return JSON.stringify({url: location.href, taClass: ta ? ta.className : null, inChatInput: ta ? !!ta.closest('.chat-input') : false, sends: sends.slice(0, 5), sendBtnClass: sendBtn ? sendBtn.className : null}); })()""") log("== 3) 聊天页 ==", r3) c.close() lite.close_tab(ctab["id"]) else: log("== 3) 无 chatHref,跳过 ==") log("== DONE ==") print("probe7 done ->", OUT) if __name__ == "__main__": main()