"""探测职位列表页真实结构:搜索框 / 筛选栏 / 下拉加载 / 城市下拉。 输出写入 data/debug/job_page_probe.txt(UTF-8,避免控制台乱码)。""" import json import time from pathlib import Path from client.browser.cdp_lite import LiteCDP OUT = Path(__file__).resolve().parent / "data" / "debug" / "job_page_probe.txt" OUT.parent.mkdir(parents=True, exist_ok=True) _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) 页面结构 ---- r1 = lite.evaluate(ws, """(() => { const q = s => document.querySelector(s); const inp = q('.ipt-search') || q('.input-wrap input') || q('input[placeholder*="搜索"]'); const cards = document.querySelectorAll('.job-card-wrapper'); const list = q('.job-list-box') || q('.job-list') || (cards[0] ? cards[0].parentElement : null); const fb = q('.filter-bar') || q('.search-condition') || q('[class*="condition"]') || q('[class*="filter"]'); return JSON.stringify({ url: location.href, title: document.title, searchInputClass: inp ? inp.className : null, searchInputPlaceholder: inp ? inp.placeholder : null, hasSearchBtn: !!q('.btn-search'), cardCount: cards.length, listClass: list ? list.className : null, filterClass: fb ? fb.className : null, filterHTML: fb ? fb.outerHTML.replace(/\\s+/g, ' ').slice(0, 5000) : null }); })()""") log("== 1) 页面结构 ==") log(r1) # ---- 2) 搜索交互:输入 java 点搜索 ---- r2 = lite.evaluate(ws, """(() => { const inp = document.querySelector('.ipt-search') || document.querySelector('.input-wrap input') || document.querySelector('input[placeholder*="搜索"]'); if (!inp) return JSON.stringify({ok: false, reason: 'no input'}); const setter = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, 'value').set; setter.call(inp, 'java'); inp.dispatchEvent(new Event('input', {bubbles: true})); const btn = document.querySelector('.btn-search'); if (btn) btn.click(); return JSON.stringify({ok: true, clicked: !!btn}); })()""") log("== 2) 搜索操作 ==", r2) time.sleep(5) r2b = lite.evaluate(ws, """(() => JSON.stringify({ url: location.href, cardCount: document.querySelectorAll('.job-card-wrapper').length, firstTitle: (document.querySelector('.job-card-wrapper .job-name') || {}).innerText || '' }))()""") log("== 2b) 搜索后 ==", r2b) # ---- 3) 下拉加载:滚到底看卡片数是否增加 ---- r3 = lite.evaluate(ws, """(() => { const before = document.querySelectorAll('.job-card-wrapper').length; window.scrollTo(0, document.body.scrollHeight); return JSON.stringify({before: before, scrollY: window.scrollY, bodyH: document.body.scrollHeight}); })()""") log("== 3) 滚动前 ==", r3) time.sleep(4) r3b = lite.evaluate(ws, """(() => { const after1 = document.querySelectorAll('.job-card-wrapper').length; window.scrollTo(0, document.body.scrollHeight); return JSON.stringify({after1: after1}); })()""") time.sleep(4) r3c = lite.evaluate(ws, """(() => JSON.stringify({ after2: document.querySelectorAll('.job-card-wrapper').length, url: location.href }))()""") log("== 3b) 滚动两次后 ==", r3b, r3c) # ---- 4) 城市下拉结构 ---- r4 = lite.evaluate(ws, """(() => { const fb = document.querySelector('.filter-bar') || document.querySelector('[class*="condition"]') || document.querySelector('[class*="filter"]'); if (!fb) return JSON.stringify({ok: false}); // 找到城市筛选节点(含“城市”或定位图标的第一个 dropdown) const nodes = [...fb.querySelectorAll('li, .dropdown, [class*="city"]')]; const cityNode = nodes.find(n => /城市|北京|上海|广州|深圳/.test(n.innerText || '') && n.innerText.length < 20); if (cityNode) cityNode.click(); return JSON.stringify({ok: true, cityNodeClass: cityNode ? cityNode.className : null, cityText: cityNode ? cityNode.innerText : null}); })()""") log("== 4) 城市节点点击 ==", r4) time.sleep(1.5) r4b = lite.evaluate(ws, """(() => { const panel = document.querySelector('.city-select, .dropdown-menu, [class*="city"][class*="select"], .position-city'); const anyPanel = panel || [...document.querySelectorAll('div')].find(d => d.className && /city/i.test(d.className) && d.offsetHeight > 100); return JSON.stringify({ panelClass: anyPanel ? anyPanel.className : null, panelHTML: anyPanel ? anyPanel.outerHTML.replace(/\\s+/g, ' ').slice(0, 4000) : null }); })()""") log("== 4b) 城市下拉面板 ==", r4b) # ---- 5) 薪资下拉结构 ---- r5 = lite.evaluate(ws, """(() => { const fb = document.querySelector('.filter-bar') || document.querySelector('[class*="condition"]'); if (!fb) return JSON.stringify({ok: false}); const nodes = [...fb.querySelectorAll('li, .dropdown')]; const salaryNode = nodes.find(n => /薪资待遇/.test(n.innerText || '')); if (salaryNode) salaryNode.click(); return JSON.stringify({ok: true, found: !!salaryNode}); })()""") log("== 5) 薪资节点点击 ==", r5) time.sleep(1.5) r5b = lite.evaluate(ws, """(() => { const panels = [...document.querySelectorAll('div,ul')].filter(d => d.offsetHeight > 50 && /面议|K以下|K/.test(d.innerText || '') && (d.innerText || '').length < 300); const p = panels[panels.length - 1]; return JSON.stringify({panelClass: p ? p.className : null, panelText: p ? p.innerText.replace(/\\s+/g, ' ').slice(0, 500) : null}); })()""") log("== 5b) 薪资下拉面板 ==", r5b) lite.close_tab(tab["id"]) log("== DONE ==") print("probe done ->", OUT) if __name__ == "__main__": main()