Files
jianli-boss/client/debug_job_probe4.py
2026-08-25 22:53:57 +08:00

167 lines
6.1 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""四次探测:
A. Network 域被动监听站点自己的 joblist XHR,验证能否拿到明文薪资(页面无感知)
B. 职位详情页结构:沟通/投递按钮、JD 容器、聊天输入框
"""
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_probe4.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:
"""极简 CDP 会话:支持命令调用与事件泵"""
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")
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()
# ---------- A. 列表页 XHR 监听 ----------
tab = lite.open_tab("https://www.zhipin.com/web/geek/jobs")
s = Session(tab["webSocketDebuggerUrl"])
s.call("Network.enable")
s.call("Runtime.evaluate", {"expression": """(() => {
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;
})()"""})
req_urls: dict[str, str] = {}
finished: list[str] = []
for msg in s.pump(8):
m = msg.get("method")
if m == "Network.responseReceived":
u = msg["params"]["response"]["url"]
if "joblist" in u or "search" in u:
req_urls[msg["params"]["requestId"]] = u
elif m == "Network.loadingFinished":
rid = msg["params"]["requestId"]
if rid in req_urls:
finished.append(rid)
log("== A1) 捕获的 XHR ==", {req_urls[r] for r in finished} or req_urls.values())
for rid in finished[:2]:
body = s.call("Network.getResponseBody", {"requestId": rid}) or {}
try:
data = json.loads(body.get("body", ""))
zp = data.get("zpData", {})
jobs = zp.get("jobList") or zp.get("dataList") or []
log("== A2) joblist 响应 ==", json.dumps({
"keys": list(zp.keys())[:15],
"jobCount": len(jobs),
"firstJob": jobs[0] if jobs else None,
}, ensure_ascii=False)[:3000])
except Exception as e:
log("== A2) 解析失败 ==", e, body.get("body", "")[:300])
# 列表页稳定性(Network.enable 后是否还被风控重置)
stable = s.call("Runtime.evaluate", {
"expression": "JSON.stringify({url: location.href, cards: document.querySelectorAll('.job-card-wrap').length})"
})
log("== A3) 列表页稳定性 ==", stable)
# 取第一个职位 ID 供 B 使用
first_href = s.call("Runtime.evaluate", {
"expression": "(document.querySelector('.job-card-wrap a.job-name') || {}).getAttribute ? document.querySelector('.job-card-wrap a.job-name').getAttribute('href') : ''"
})
s.close()
lite.close_tab(tab["id"])
log("== A4) 首个职位 href ==", first_href)
# ---------- B. 详情页结构 ----------
if not first_href:
log("== DONE(无职位,跳过 B)==")
return
dtab = lite.open_tab("https://www.zhipin.com" + first_href)
d = Session(dtab["webSocketDebuggerUrl"])
time.sleep(5)
r1 = d.call("Runtime.evaluate", {"expression": """(() => {
const btns = [...document.querySelectorAll('a,button')]
.filter(e => /立即沟通|投递简历|收藏/.test(e.innerText || '') && e.offsetHeight > 0)
.map(e => ({tag: e.tagName, cls: e.className, href: e.getAttribute('href') || ''}));
const cands = {};
['.job-detail-section', '.job-sec-text', '.detail-content', '.job-detail .text', '[class*="job-detail"]']
.forEach(sel => { const el = document.querySelector(sel); cands[sel] = el ? el.innerText.slice(0, 60) : null; });
return JSON.stringify({url: location.href, btns: btns, cands: cands});
})()"""})
log("== B1) 详情页按钮/JD ==", r1)
# 点击立即沟通,看聊天结构
r2 = d.call("Runtime.evaluate", {"expression": """(() => {
const btn = [...document.querySelectorAll('a,button')].find(e => /立即沟通/.test(e.innerText || '') && e.offsetHeight > 0);
if (!btn) return JSON.stringify({ok: false});
btn.click();
return JSON.stringify({ok: true});
})()"""})
log("== B2) 点击立即沟通 ==", r2)
time.sleep(4)
r3 = d.call("Runtime.evaluate", {"expression": """(() => {
const ta = document.querySelector('textarea');
const sends = [...document.querySelectorAll('a,button,div')]
.filter(e => /发送/.test(e.innerText || '') && (e.innerText || '').length < 6 && e.offsetHeight > 0)
.map(e => ({tag: e.tagName, cls: e.className}));
return JSON.stringify({
url: location.href,
taClass: ta ? ta.className : null,
taSelector: ta ? (ta.closest('.chat-input') ? '.chat-input textarea' : 'textarea') : null,
sends: sends.slice(0, 5)
});
})()"""})
log("== B3) 聊天页结构 ==", r3)
d.close()
lite.close_tab(dtab["id"])
log("== DONE ==")
print("probe4 done ->", OUT)
if __name__ == "__main__":
main()