107 lines
4.0 KiB
Python
107 lines
4.0 KiB
Python
"""三次探测:薪资反爬字符 / 卡片数据属性 / 右侧详情面板沟通按钮 / 薪资下拉交互"""
|
|
import time
|
|
from pathlib import Path
|
|
|
|
from client.browser.cdp_lite import LiteCDP
|
|
|
|
OUT = Path(__file__).resolve().parent / "data" / "debug" / "job_page_probe3.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)
|
|
|
|
# 先搜索 java
|
|
lite.evaluate(ws, """(() => {
|
|
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;
|
|
})()""")
|
|
time.sleep(5)
|
|
|
|
# 1) 薪资文本码点 + 卡片 dataset
|
|
r1 = lite.evaluate(ws, """(() => {
|
|
const sal = document.querySelector('.job-salary');
|
|
const card = document.querySelector('.job-card-wrap');
|
|
const li = document.querySelector('.job-card-box');
|
|
return JSON.stringify({
|
|
salaryText: sal ? sal.innerText : null,
|
|
salaryCodes: sal ? [...sal.innerText].map(c => c.codePointAt(0).toString(16)) : null,
|
|
cardDataset: card ? Object.assign({}, card.dataset) : null,
|
|
liDataset: li ? Object.assign({}, li.dataset) : null,
|
|
liAttrs: li ? [...li.attributes].map(a => a.name + '=' + a.value).slice(0, 10) : null
|
|
});
|
|
})()""")
|
|
log("== 1) 薪资码点/卡片属性 ==", r1)
|
|
|
|
# 2) 点击第一张卡片,看右侧详情面板
|
|
r2 = lite.evaluate(ws, """(() => {
|
|
const card = document.querySelector('.job-card-wrap');
|
|
if (!card) return JSON.stringify({ok: false});
|
|
card.click();
|
|
return JSON.stringify({ok: true});
|
|
})()""")
|
|
log("== 2) 点击卡片 ==", r2)
|
|
time.sleep(3)
|
|
r2b = lite.evaluate(ws, """(() => {
|
|
const btns = [...document.querySelectorAll('a,button')]
|
|
.filter(e => /立即沟通|投递简历|收藏/.test(e.innerText || '') && e.offsetHeight > 0)
|
|
.map(e => ({tag: e.tagName, cls: e.className, ka: e.getAttribute('ka'), text: (e.innerText || '').trim()}));
|
|
const jd = document.querySelector('.job-detail-section, .job-sec-text, [class*="job-detail"], [class*="desc"]');
|
|
return JSON.stringify({
|
|
btns: btns,
|
|
jdClass: jd ? jd.className : null,
|
|
jdTextHead: jd ? jd.innerText.slice(0, 120) : null
|
|
});
|
|
})()""")
|
|
log("== 2b) 详情面板 ==", r2b)
|
|
|
|
# 3) 薪资下拉:hover+click 打开,点 10-20K
|
|
r3 = lite.evaluate(ws, """(() => {
|
|
const sels = [...document.querySelectorAll('.condition-filter-select')];
|
|
const salary = sels.find(s => /薪资待遇/.test(s.innerText || ''));
|
|
if (!salary) return JSON.stringify({ok: false, reason: 'no salary select'});
|
|
const cur = salary.querySelector('.current-select');
|
|
cur.dispatchEvent(new MouseEvent('mouseenter', {bubbles: true}));
|
|
cur.dispatchEvent(new MouseEvent('mouseover', {bubbles: true}));
|
|
cur.click();
|
|
return JSON.stringify({ok: true});
|
|
})()""")
|
|
log("== 3) 打开薪资下拉 ==", r3)
|
|
time.sleep(1.5)
|
|
r3b = lite.evaluate(ws, """(() => {
|
|
const li = [...document.querySelectorAll('.condition-filter-select li')]
|
|
.find(l => /10-20K/.test(l.innerText || ''));
|
|
if (!li) return JSON.stringify({ok: false, reason: 'no option visible'});
|
|
li.click();
|
|
return JSON.stringify({ok: true});
|
|
})()""")
|
|
log("== 3b) 点击10-20K ==", r3b)
|
|
time.sleep(4)
|
|
r3c = lite.evaluate(ws, """(() => JSON.stringify({
|
|
url: location.href,
|
|
cardCount: document.querySelectorAll('.job-card-wrap').length
|
|
}))()""")
|
|
log("== 3c) 薪资筛选后 ==", r3c)
|
|
|
|
lite.close_tab(tab["id"])
|
|
log("== DONE ==")
|
|
print("probe3 done ->", OUT)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|