"""二次探测:职位卡片真实类名 / 搜索按钮 / 搜索触发方式""" import time from pathlib import Path from client.browser.cdp_lite import LiteCDP OUT = Path(__file__).resolve().parent / "data" / "debug" / "job_page_probe2.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") def main(): lite = LiteCDP() tab = lite.open_tab("https://www.zhipin.com/web/geek/jobs") ws = tab["webSocketDebuggerUrl"] time.sleep(7) # 1) 搜索区域结构(input 的上层容器) r1 = lite.evaluate(ws, """(() => { const inp = document.querySelector('input[placeholder*="搜索"]'); let box = inp; for (let i = 0; i < 3 && box; i++) box = box.parentElement; const btns = [...document.querySelectorAll('a,button,div,span')] .filter(e => (e.innerText || '').trim() === '搜索' && e.offsetHeight > 0) .map(e => ({tag: e.tagName, cls: e.className, ka: e.getAttribute('ka')})); return JSON.stringify({ searchBoxHTML: box ? box.outerHTML.replace(/\\s+/g, ' ').slice(0, 2500) : null, searchBtns: btns }); })()""") log("== 1) 搜索区域 ==", r1) # 2) 卡片真实类名:找所有 class 含 job/card 的元素 r2 = lite.evaluate(ws, """(() => { const set = {}; document.querySelectorAll('[class*="job-card"], [class*="card-"]').forEach(e => { set[e.className] = (set[e.className] || 0) + 1; }); const list = document.querySelector('.job-list-box, .job-list, .search-job-result, .job-content'); const main = document.querySelector('main') || document.querySelector('.page-content') || document.querySelector('#wrap'); return JSON.stringify({ cardClasses: Object.keys(set).slice(0, 20), listClass: list ? list.className : null, mainSnippet: (main || document.body).outerHTML.replace(/\\s+/g, ' ').slice(0, 3500) }); })()""") log("== 2) 卡片类名/主区域 ==", r2) # 3) 输入 java + 回车触发搜索 r3 = lite.evaluate(ws, """(() => { const inp = document.querySelector('input[placeholder*="搜索"]'); if (!inp) return JSON.stringify({ok: false}); 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 JSON.stringify({ok: true}); })()""") log("== 3) 回车搜索 ==", r3) time.sleep(5) r3b = lite.evaluate(ws, """(() => { const cards = document.querySelectorAll('[class*="job-card"]'); return JSON.stringify({ url: location.href, cardCount: cards.length, firstCardHTML: cards[0] ? cards[0].outerHTML.replace(/\\s+/g, ' ').slice(0, 2500) : null }); })()""") log("== 3b) 回车后 ==", r3b) # 4) 再滚一次看增量 lite.evaluate(ws, "window.scrollTo(0, document.body.scrollHeight)") time.sleep(4) r4 = lite.evaluate(ws, """(() => JSON.stringify({ cardCount: document.querySelectorAll('[class*="job-card"]').length, url: location.href }))()""") log("== 4) 滚动后 ==", r4) lite.close_tab(tab["id"]) log("== DONE ==") print("probe2 done ->", OUT) if __name__ == "__main__": main()