76 lines
2.8 KiB
Python
76 lines
2.8 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""
|
|
验证:Playwright attach + 反调试注入 后能否稳定抓取简历页 HTML。
|
|
用法:python -m client.debug_resume_dom
|
|
"""
|
|
import time
|
|
from pathlib import Path
|
|
|
|
from playwright.sync_api import sync_playwright
|
|
|
|
from .browser.manager import BrowserManager
|
|
|
|
OUT_DIR = Path(__file__).resolve().parent / "data" / "debug"
|
|
|
|
# 反调试注入:Boss 风控用 debugger 陷阱/堆栈特征检测 CDP,逐一屏蔽
|
|
STEALTH_JS = """
|
|
(() => {
|
|
// 1. 屏蔽 debugger 陷阱(构造器形式与直接语句)
|
|
const _Function = Function;
|
|
const handler = {
|
|
construct(target, args) {
|
|
if (args.length && /debugger/.test(String(args[args.length - 1]))) {
|
|
args[args.length - 1] = 'return null;';
|
|
}
|
|
return new (Function.prototype.bind.apply(target, [null, ...args]))();
|
|
},
|
|
};
|
|
try { window.Function = new Proxy(_Function, handler); } catch (e) {}
|
|
// 2. 抹掉 webdriver 特征
|
|
Object.defineProperty(navigator, 'webdriver', { get: () => undefined });
|
|
// 3. 拦截跳往 about:blank 的风控重置
|
|
const origAssign = window.location.assign, origReplace = window.location.replace;
|
|
try {
|
|
window.location.assign = function (u) { if (/about:blank/.test(u)) return; return origAssign.call(window.location, u); };
|
|
window.location.replace = function (u) { if (/about:blank/.test(u)) return; return origReplace.call(window.location, u); };
|
|
} catch (e) {}
|
|
})();
|
|
"""
|
|
|
|
|
|
def main():
|
|
OUT_DIR.mkdir(parents=True, exist_ok=True)
|
|
manager = BrowserManager()
|
|
with sync_playwright() as pw:
|
|
browser = pw.chromium.connect_over_cdp(manager.cdp_url or "http://127.0.0.1:9222")
|
|
context = browser.contexts[0]
|
|
context.add_init_script(STEALTH_JS)
|
|
page = context.new_page()
|
|
try:
|
|
print("[1] 带反调试注入打开简历页", flush=True)
|
|
page.goto("https://www.zhipin.com/web/geek/resume",
|
|
wait_until="domcontentloaded", timeout=30000)
|
|
for wait_s in (5, 10, 15):
|
|
time.sleep(5)
|
|
print(f"[2 {wait_s}s] URL={page.url}", flush=True)
|
|
if page.url == "about:blank":
|
|
print(" !! 仍被风控重置为 about:blank", flush=True)
|
|
continue
|
|
try:
|
|
html = page.content()
|
|
(OUT_DIR / f"resume_dom_{wait_s}s.html").write_text(html, encoding="utf-8")
|
|
page.screenshot(path=str(OUT_DIR / f"resume_dom_{wait_s}s.png"), full_page=True)
|
|
print(f" HTML {len(html)} 字符已保存", flush=True)
|
|
except Exception as e:
|
|
print(f" content 失败:{e}", flush=True)
|
|
finally:
|
|
try:
|
|
page.close()
|
|
except Exception:
|
|
pass
|
|
print("[3] 完成", flush=True)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|