"""排查:职位卡片中"猎头"标识的真实 DOM,验证猎头识别逻辑""" import time from pathlib import Path from client.browser.cdp_lite import LiteCDP, CDPSession from client.browser.manager import BrowserManager OUT = Path(__file__).resolve().parent / "data" / "debug" / "猎头标识排查.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") _DUMP_JS = """(() => { const cards = [...document.querySelectorAll('.job-card-wrap')]; const out = {total: cards.length, hits: []}; cards.forEach((el, i) => { // 收集卡片内所有自身文本含"猎头"的元素(不含后代文本,定位最小节点) const own = n => [...n.childNodes] .filter(c => c.nodeType === 3).map(c => c.textContent).join(''); const found = [...el.querySelectorAll('*')].filter(n => own(n).includes('猎头') || (n.children.length === 0 && (n.innerText || n.textContent || '').includes('猎头'))); if (found.length || el.innerText.includes('猎头')) { out.hits.push({ i, title: (el.querySelector('a.job-name') || {}).innerText || '', cardHasText: el.innerText.includes('猎头'), nodes: found.slice(0, 5).map(n => ({ tag: n.tagName, cls: String(n.className), text: (n.innerText || '').trim().slice(0, 30), html: n.outerHTML.replace(/\\s+/g, ' ').slice(0, 220), })), // 与 applicant._CARDS_JS 一致的判定结果 exact: /猎头/.test(el.innerText), }); } }); return JSON.stringify(out); })()""" def main(): BrowserManager().ensure_browser_started() lite = LiteCDP() tab = lite.open_tab("https://www.zhipin.com/web/geek/jobs") time.sleep(8) s = CDPSession(tab["webSocketDebuggerUrl"]) log("卡片总数:", s.eval_js("document.querySelectorAll('.job-card-wrap').length")) log("猎头标识排查:", s.eval_js(_DUMP_JS)) s.close() lite.close_tab(tab["id"]) log("完成") print("排查完成 ->", OUT) if __name__ == "__main__": main()