基本完成
This commit is contained in:
@@ -0,0 +1,165 @@
|
||||
"""九次探测:回车搜索 / 点卡片捕获 detail.json / 右侧面板全量结构 / 可信点击沟通"""
|
||||
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_probe9.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*="搜索"]');
|
||||
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}));
|
||||
inp.dispatchEvent(new KeyboardEvent('keyup', {key: 'Enter', code: 'Enter', keyCode: 13, which: 13, bubbles: true}));
|
||||
return true;
|
||||
})()"""
|
||||
|
||||
|
||||
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(SEARCH_JS)
|
||||
time.sleep(5)
|
||||
log("== 1) 搜索后 URL ==", s.eval_js("location.href"))
|
||||
|
||||
# 点第二张卡片 + 捕获 detail.json
|
||||
s.eval_js("""(() => {
|
||||
const c = document.querySelectorAll('.job-card-wrap')[1];
|
||||
if (c) c.click();
|
||||
return !!c;
|
||||
})()""")
|
||||
req_urls: dict[str, str] = {}
|
||||
bodies: list[str] = []
|
||||
for msg in s.pump(4):
|
||||
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:
|
||||
bodies.append(s.call("Network.getResponseBody", {"requestId": rid}).get("body", ""))
|
||||
log("== 2) detail.json 数量 ==", len(bodies))
|
||||
for body in bodies[:1]:
|
||||
try:
|
||||
zp = json.loads(body).get("zpData", {})
|
||||
ji = zp.get("jobInfo") or zp
|
||||
log("== 2b) 字段 ==", json.dumps({
|
||||
"salary": ji.get("salaryDesc") or ji.get("salary"),
|
||||
"jdHead": (ji.get("jobDesc") or "")[:80],
|
||||
"jiKeys": list(ji.keys())[:35],
|
||||
}, ensure_ascii=False)[:1800])
|
||||
except Exception as e:
|
||||
log("== 2b) 解析失败 ==", e, body[:300])
|
||||
|
||||
# 右侧面板全量结构:op-btn-chat 的祖先面板
|
||||
r3 = s.eval_js("""(() => {
|
||||
const b = document.querySelector('.op-btn-chat');
|
||||
if (!b) return JSON.stringify({ok: false});
|
||||
let p = b;
|
||||
for (let i = 0; i < 5 && p.parentElement; i++) p = p.parentElement;
|
||||
return JSON.stringify({ok: true, panelClass: p.className,
|
||||
html: p.outerHTML.replace(/\\s+/g, ' ').slice(0, 3000)});
|
||||
})()""")
|
||||
log("== 3) 右侧面板 ==", 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"]]
|
||||
log("== 4) 新标签 ==", [t.get("url") for t in new_tabs])
|
||||
|
||||
if new_tabs:
|
||||
c = Session(new_tabs[0]["webSocketDebuggerUrl"])
|
||||
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()
|
||||
lite.close_tab(new_tabs[0]["id"])
|
||||
|
||||
s.close()
|
||||
lite.close_tab(tab["id"])
|
||||
log("== DONE ==")
|
||||
print("probe9 done ->", OUT)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user