103 lines
3.4 KiB
Python
103 lines
3.4 KiB
Python
"""
|
||
AI 简历抓取模块(低痕迹 CDP 版)
|
||
Boss直聘 的简历页是一份"在线简历"的分板块编辑页(不是一份份的简历列表),
|
||
板块以 id 区分:userinfo/summary/purpose/history/project/education/
|
||
clubExp/professional-skill。
|
||
通过原生 CDP 通道(不 attach 页面)读取各板块 innerText,拼装成结构化简历。
|
||
"""
|
||
import json
|
||
|
||
from loguru import logger
|
||
|
||
from .cdp_lite import LiteCDP
|
||
from .manager import BOSS_RESUME_PAGE
|
||
|
||
# 在页面上一次性抽取各板块文本(返回 JSON 字符串)
|
||
_SECTION_JS = """(() => {
|
||
const ids = ['userinfo', 'summary', 'purpose', 'history', 'project',
|
||
'education', 'clubExp', 'professional-skill'];
|
||
const out = {};
|
||
for (const id of ids) {
|
||
const el = document.getElementById(id);
|
||
if (el) out[id] = el.innerText;
|
||
}
|
||
return JSON.stringify(out);
|
||
})()"""
|
||
|
||
# 板块标题(用于拼装简历文本)
|
||
SECTION_TITLES = {
|
||
"userinfo": "基本信息",
|
||
"summary": "个人优势",
|
||
"purpose": "期望职位",
|
||
"history": "工作经历",
|
||
"project": "项目经历",
|
||
"education": "教育经历",
|
||
"clubExp": "社团/组织经历",
|
||
"professional-skill": "专业技能",
|
||
}
|
||
|
||
|
||
class ResumeScraper:
|
||
"""抓取 Boss直聘 账号内的在线简历"""
|
||
|
||
def __init__(self, manager, ai_client=None):
|
||
self.manager = manager
|
||
self.ai = ai_client
|
||
self.cdp = LiteCDP()
|
||
|
||
def scrape_all(self) -> list[dict]:
|
||
"""
|
||
抓取在线简历。
|
||
返回格式:[{boss_resume_id, title, position, content}, ...]
|
||
"""
|
||
logger.info("正在打开 Boss直聘 简历页...")
|
||
# 浏览器可能已被手动关闭,先确保拉起(复用登录态 profile)
|
||
self.manager.ensure_browser_started()
|
||
tab = self.cdp.open_tab(BOSS_RESUME_PAGE)
|
||
tab_id, ws_url = tab["id"], tab["webSocketDebuggerUrl"]
|
||
try:
|
||
import time
|
||
time.sleep(8) # 等 SPA 渲染完成
|
||
raw = self.cdp.evaluate(ws_url, _SECTION_JS)
|
||
finally:
|
||
self.cdp.close_tab(tab_id)
|
||
|
||
if not raw:
|
||
logger.warning("未能从简历页读取到内容")
|
||
return []
|
||
|
||
try:
|
||
sections = json.loads(raw)
|
||
except Exception as e:
|
||
logger.warning(f"简历板块数据解析失败:{e}")
|
||
return []
|
||
|
||
if not sections:
|
||
logger.warning("简历页未找到任何板块,请确认已登录")
|
||
return []
|
||
|
||
# 拼装结构化简历文本
|
||
parts = []
|
||
for key, title in SECTION_TITLES.items():
|
||
text = (sections.get(key) or "").strip()
|
||
if text:
|
||
parts.append(f"【{title}】\n{text}")
|
||
content = "\n\n".join(parts)
|
||
|
||
# 从基本信息板块首行提取姓名作为标题
|
||
userinfo = (sections.get("userinfo") or "").strip()
|
||
name = userinfo.splitlines()[0].strip() if userinfo else ""
|
||
title = f"{name}的在线简历" if name else "我的在线简历"
|
||
|
||
# 用 AI 整理内容(可选)
|
||
if content and self.ai and self.ai.available():
|
||
content = self.ai.clean_resume(content)
|
||
|
||
logger.success(f"简历抓取完成:{title},{len(content)} 字符")
|
||
return [{
|
||
"boss_resume_id": None, # Boss直聘 前端不暴露简历 ID
|
||
"title": title,
|
||
"position": None,
|
||
"content": content or None,
|
||
}]
|