基本完成
This commit is contained in:
@@ -0,0 +1,128 @@
|
||||
"""验证顶部"期望搜索条件"栏:结构 / 点击 Java(北京) 后列表是否加载 / 滚动分页"""
|
||||
import json
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
import websocket
|
||||
|
||||
from client.browser.cdp_lite import LiteCDP
|
||||
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")
|
||||
|
||||
|
||||
class 会话:
|
||||
def __init__(self, ws_url):
|
||||
self.ws = websocket.create_connection(ws_url, timeout=30)
|
||||
self._id = 0
|
||||
|
||||
def 调用(self, method, params=None, timeout=10):
|
||||
self._id += 1
|
||||
mid = self._id
|
||||
self.ws.send(json.dumps({"id": mid, "method": method, "params": params or {}}))
|
||||
self.ws.settimeout(timeout)
|
||||
while True:
|
||||
msg = json.loads(self.ws.recv())
|
||||
if msg.get("id") == mid:
|
||||
return msg.get("result") or {}
|
||||
|
||||
def 求值(self, expr, timeout=10):
|
||||
return (self.调用("Runtime.evaluate",
|
||||
{"expression": expr, "returnByValue": True})
|
||||
.get("result", {}).get("value"))
|
||||
|
||||
def 泵(self, duration):
|
||||
self.ws.settimeout(0.5)
|
||||
end = time.time() + duration
|
||||
while time.time() < end:
|
||||
try:
|
||||
yield json.loads(self.ws.recv())
|
||||
except Exception:
|
||||
continue
|
||||
|
||||
def 关闭(self):
|
||||
try:
|
||||
self.ws.close()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
def main():
|
||||
BrowserManager().ensure_browser_started()
|
||||
lite = LiteCDP()
|
||||
|
||||
tab = lite.open_tab("https://www.zhipin.com/web/geek/jobs")
|
||||
time.sleep(8)
|
||||
s = 会话(tab["webSocketDebuggerUrl"])
|
||||
s.调用("Network.enable")
|
||||
|
||||
# 1) 期望条件栏结构
|
||||
r1 = s.求值("""(() => {
|
||||
const items = [...document.querySelectorAll('a,div,span,li')]
|
||||
.filter(e => /^[^()()]{1,20}[((][^()()]{1,10}[))]$/.test((e.innerText || '').trim())
|
||||
&& e.offsetHeight > 0 && e.children.length === 0)
|
||||
.map(e => ({tag: e.tagName, cls: String(e.className).slice(0, 60), text: e.innerText.trim()}));
|
||||
const first = items[0];
|
||||
let bar = null;
|
||||
if (first) {
|
||||
const el = [...document.querySelectorAll('*')].find(d => d.className &&
|
||||
String(d.className).includes(first.cls.split(' ')[0]) && d.querySelectorAll('*').length > 2);
|
||||
}
|
||||
const wrap = items.length ? items[0] && (() => {
|
||||
let p = document.querySelector('.' + (first.cls.split(' ')[0] || 'x'));
|
||||
for (let i = 0; i < 3 && p && p.parentElement; i++) p = p.parentElement;
|
||||
return p ? {cls: String(p.className).slice(0, 80), html: p.outerHTML.replace(/\\s+/g, ' ').slice(0, 1500)} : null;
|
||||
})() : null;
|
||||
return JSON.stringify({items: items, wrap: wrap});
|
||||
})()""")
|
||||
log("1) 期望条件栏:", r1)
|
||||
|
||||
# 2) 点击 Java(北京)
|
||||
r2 = s.求值("""(() => {
|
||||
const el = [...document.querySelectorAll('a,div,span,li')]
|
||||
.find(e => (e.innerText || '').trim().includes('(北京)') && e.offsetHeight > 0 && e.children.length === 0);
|
||||
if (!el) return JSON.stringify({ok: false});
|
||||
el.click();
|
||||
return JSON.stringify({ok: true, text: el.innerText.trim()});
|
||||
})()""")
|
||||
log("2) 点击:", r2)
|
||||
time.sleep(5)
|
||||
log("2b) 点击后:", s.求值("""JSON.stringify({
|
||||
地址: location.href,
|
||||
卡片数: document.querySelectorAll('.job-card-wrap').length})"""))
|
||||
|
||||
# 3) 滚动分页 + 薪资明文
|
||||
请求: dict[str, str] = {}
|
||||
s.求值("window.scrollTo(0, document.body.scrollHeight)")
|
||||
薪资 = []
|
||||
for msg in s.泵(6):
|
||||
m = msg.get("method")
|
||||
if m == "Network.responseReceived" and "list.json" in msg["params"]["response"]["url"]:
|
||||
请求[msg["params"]["requestId"]] = msg["params"]["response"]["url"]
|
||||
elif m == "Network.loadingFinished" and msg["params"]["requestId"] in 请求:
|
||||
body = s.调用("Network.getResponseBody",
|
||||
{"requestId": msg["params"]["requestId"]}).get("body", "")
|
||||
try:
|
||||
jobs = json.loads(body).get("zpData", {}).get("jobList") or []
|
||||
薪资.extend(j.get("salaryDesc") for j in jobs[:5])
|
||||
except Exception:
|
||||
pass
|
||||
log("3) 滚动后卡片数:", s.求值("document.querySelectorAll('.job-card-wrap').length"),
|
||||
" 分页薪资明文:", 薪资)
|
||||
|
||||
s.关闭()
|
||||
lite.close_tab(tab["id"])
|
||||
log("完成")
|
||||
print("验证完成 ->", OUT)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user