"""排查猎头角标/筛选框真实 DOM(交互式): 脚本打开 Boss 职位页并保持标签页打开,你手动点开页面上"猎头"相关的方框, 脚本每 5 秒自动抓一次页面状态(所有自身文本含"猎头"的元素 + 命中的卡片 HTML), 连续 3 轮稳定抓到后自动结束并关闭标签页;最多等 4 分钟。 用法:python -m client.debug_hunter_wait [搜索词] [城市码] 结果:client/data/debug/猎头点击排查.txt """ import sys 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" QUERY = sys.argv[1] if len(sys.argv) > 1 else "" CITY = sys.argv[2] if len(sys.argv) > 2 else "" MAX_ROUNDS = 48 # 48 x 5s = 4 分钟 INTERVAL = 5 # 每轮间隔秒数 _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") # 全页面中"自身文本"含猎头的元素(定位最小节点:角标/筛选框文字本身) _OWN_TEXT_JS = """(() => { const out = []; for (const n of document.querySelectorAll('*')) { const own = [...n.childNodes] .filter(c => c.nodeType === 3) .map(c => c.textContent).join('').trim(); if (own.includes('猎头')) { out.push({ tag: n.tagName, cls: String(n.className).slice(0, 120), text: own.slice(0, 40), html: n.outerHTML.replace(/\\s+/g, ' ').slice(0, 350), }); } } return JSON.stringify(out.slice(0, 20)); })()""" _SUMMARY_JS = """(() => { const cards = [...document.querySelectorAll('.job-card-wrap')]; return JSON.stringify({ total: cards.length, withText: cards.reduce((acc, el, i) => (/猎头/.test(el.innerText) ? acc.concat(i) : acc), []), }); })()""" def main(): BrowserManager().ensure_browser_started() lite = LiteCDP() url = "https://www.zhipin.com/web/geek/jobs" if QUERY: url += f"?query={QUERY}" if CITY: url += f"&city={CITY}" tab = lite.open_tab(url) print(f"页面已打开:{url}") print(">>> 请在页面上点开猎头相关的方框/筛选,脚本会自动抓取,抓稳后自动结束 <<<") time.sleep(8) s = CDPSession(tab["webSocketDebuggerUrl"]) s.call("Network.enable") stable = 0 dumped_card = False for r in range(1, MAX_ROUNDS + 1): try: summary = s.eval_js(_SUMMARY_JS) hits = s.eval_js(_OWN_TEXT_JS) except Exception as e: log(f"----- 第{r}轮:页面状态读取失败({e}),等待重试 -----") time.sleep(INTERVAL) continue log(f"----- 第{r}轮 -----") log("卡片概况(total/withText索引):", summary) log("自身文本含猎头的元素:", hits) found = (hits not in ("[]", "", None)) or ('"withText":[]' not in (summary or "")) if found: stable += 1 # 命中后 dump 第一张含猎头文本的卡片完整 HTML(只 dump 一次) if not dumped_card: card_html = s.eval_js("""(() => { const el = [...document.querySelectorAll('.job-card-wrap')] .find(c => /猎头/.test(c.innerText)); return el ? el.outerHTML.replace(/\\s+/g, ' ').slice(0, 2500) : ''; })()""") if card_html: log("===== 含猎头文本的卡片 HTML =====") log(card_html) dumped_card = True if stable >= 3: log(f"连续 {stable} 轮抓到猎头元素,排查完成") break else: stable = 0 time.sleep(INTERVAL) s.close() lite.close_tab(tab["id"]) log("完成") print("排查完成 ->", OUT) if __name__ == "__main__": main()