基本完成

This commit is contained in:
2026-08-25 22:53:57 +08:00
parent 05fa686749
commit 252d5b53ac
39 changed files with 3853 additions and 447 deletions
+178
View File
@@ -0,0 +1,178 @@
"""十次探测(修正版):等加载→回车搜索→点卡片捕 XHR→面板结构→可信点击沟通(查新标签+同页跳转)"""
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_probe10.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 click_at(self, x, y):
self.call("Input.dispatchMouseEvent", {
"type": "mousePressed", "x": x, "y": y, "button": "left", "clickCount": 1})
time.sleep(0.08)
self.call("Input.dispatchMouseEvent", {
"type": "mouseReleased", "x": x, "y": y, "button": "left", "clickCount": 1})
def close(self):
try:
self.ws.close()
except Exception:
pass
SEARCH_JS = """(() => {
const inp = document.querySelector('input[placeholder*="搜索"]');
if (!inp) return 'NO_INPUT';
const setter = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, 'value').set;
setter.call(inp, 'java');
inp.dispatchEvent(new Event('input', {bubbles: true}));
inp.dispatchEvent(new KeyboardEvent('keydown', {key: 'Enter', code: 'Enter', keyCode: 13, which: 13, bubbles: true}));
return 'OK';
})()"""
def main():
BrowserManager().ensure_browser_started()
lite = LiteCDP()
tab = lite.open_tab("https://www.zhipin.com/web/geek/jobs")
time.sleep(8) # 等 SPA 挂载
s = Session(tab["webSocketDebuggerUrl"])
s.call("Network.enable")
log("== 1) 搜索触发 ==", s.eval_js(SEARCH_JS))
time.sleep(5)
log("== 1b) 搜索后 URL ==", s.eval_js("location.href"))
# 点第二张卡片,捕获所有 wapi XHR
s.eval_js("""(() => {
const c = document.querySelectorAll('.job-card-wrap')[1];
if (c) c.click();
return !!c;
})()""")
req_urls: dict[str, str] = {}
bodies: dict[str, str] = {}
for msg in s.pump(4):
m = msg.get("method")
if m == "Network.responseReceived":
u = msg["params"]["response"]["url"]
if "/wapi/" in u:
req_urls[msg["params"]["requestId"]] = u
elif m == "Network.loadingFinished":
rid = msg["params"]["requestId"]
if rid in req_urls:
bodies[req_urls[rid]] = s.call(
"Network.getResponseBody", {"requestId": rid}).get("body", "")
log("== 2) 点卡片后的 XHR ==", list(bodies.keys()))
for u, body in bodies.items():
if "detail" in u:
try:
zp = json.loads(body).get("zpData", {})
ji = zp.get("jobInfo") or zp
log("== 2b) detail 字段 ==", json.dumps({
"salary": ji.get("salaryDesc") or ji.get("salary"),
"jdHead": (ji.get("jobDesc") or "")[:80],
}, ensure_ascii=False)[:1200])
except Exception as e:
log("== 2b) 解析失败 ==", e)
# 右侧面板:沟通按钮祖先链 + JD
r3 = s.eval_js("""(() => {
const b = document.querySelector('.op-btn-chat');
if (!b) return JSON.stringify({ok: false});
const chain = [];
let p = b;
for (let i = 0; i < 7 && p; i++) { chain.push(p.className || p.tagName); p = p.parentElement; }
const root = b.closest('.job-detail, .detail-content, .job-info-wrap, .recommend-result-job') || b.parentElement.parentElement.parentElement;
const jd = root ? [...root.querySelectorAll('div,section')].find(d => (d.innerText || '').trim().startsWith('职位描述')) : null;
return JSON.stringify({ok: true, chain: chain, jdClass: jd ? jd.className : null,
jdHead: jd ? jd.innerText.slice(0, 80) : null});
})()""")
log("== 3) 面板祖先链/JD ==", r3)
# 可信点击沟通:查新标签 + 同页跳转
xy = s.eval_js("""(() => {
const b = document.querySelector('.op-btn-chat');
if (!b) return null;
const r = b.getBoundingClientRect();
return JSON.stringify({x: r.x + r.width / 2, y: r.y + r.height / 2});
})()""")
before = {t["id"] for t in lite.list_tabs()}
if xy:
p = json.loads(xy)
s.click_at(int(p["x"]), int(p["y"]))
time.sleep(5)
new_tabs = [t for t in lite.list_tabs() if t["id"] not in before and t["id"] != tab["id"]]
same_url = s.eval_js("location.href")
log("== 4) 可信点击后 新标签 ==", [t.get("url") for t in new_tabs], " 同页URL ==", same_url)
target_ws = None
target_tab = None
if new_tabs:
target_ws, target_tab = new_tabs[0]["webSocketDebuggerUrl"], new_tabs[0]["id"]
elif same_url and "chat" in same_url:
target_ws, target_tab = tab["webSocketDebuggerUrl"], None
if target_ws:
c = Session(target_ws)
time.sleep(3)
r5 = c.eval_js("""(() => {
const ta = document.querySelector('textarea');
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,
sendBtnClass: sendBtn ? sendBtn.className : null});
})()""")
log("== 5) 聊天页 ==", r5)
c.close()
if target_tab:
lite.close_tab(target_tab)
s.close()
lite.close_tab(tab["id"])
log("== DONE ==")
print("probe10 done ->", OUT)
if __name__ == "__main__":
main()