修复招聘公告空正文与漏同步
This commit is contained in:
+50
-2
@@ -26,6 +26,7 @@ from .models import recruit_announcements
|
||||
SOURCE = "https://school.gxjy.sdei.edu.cn"
|
||||
LIST_ENDPOINT = SOURCE + "/sdmu/front/indexZpggList"
|
||||
DETAIL_ENDPOINT = SOURCE + "/sdmu/jiuye/zhaopingg/detail"
|
||||
DETAIL_HEADERS = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/131.0 Safari/537.36"}
|
||||
logger = logging.getLogger(__name__)
|
||||
MAX_IMAGE_EDGE = 1800
|
||||
|
||||
@@ -69,6 +70,24 @@ def html_to_text(content: str) -> str:
|
||||
return parser.text()
|
||||
|
||||
|
||||
def extract_detail_html(content: str) -> str:
|
||||
"""提取详情页服务端渲染的正文和职位列表。"""
|
||||
positions_match = re.search(
|
||||
r'<div[^>]+class="(?:[^"]*\s)?selected-positions(?:\s[^"]*)?"',
|
||||
content,
|
||||
flags=re.I,
|
||||
)
|
||||
if not positions_match:
|
||||
return ""
|
||||
start = positions_match.start()
|
||||
depth = 0
|
||||
for match in re.finditer(r'<div\b[^>]*>|</div\s*>', content[start:], flags=re.I):
|
||||
depth += 1 if match.group(0).startswith("<div") else -1
|
||||
if depth == 0:
|
||||
return content[start:start + match.end()].strip()
|
||||
return content[start:].strip()
|
||||
|
||||
|
||||
def image_url(src: str) -> str:
|
||||
src = (src or "").strip()
|
||||
if not src or src.startswith(("data:", "file:")):
|
||||
@@ -231,6 +250,23 @@ async def fetch_page(client: httpx.AsyncClient, page: int, page_size: int = 50):
|
||||
raise RuntimeError("就业平台暂时无法访问") from last_error
|
||||
|
||||
|
||||
async def fetch_detail_html(client: httpx.AsyncClient, gonggao_id: int) -> str:
|
||||
last_error: Exception | None = None
|
||||
for attempt in range(2):
|
||||
try:
|
||||
response = await asyncio.wait_for(
|
||||
client.get(f"{DETAIL_ENDPOINT}/{gonggao_id}", headers=DETAIL_HEADERS),
|
||||
timeout=15,
|
||||
)
|
||||
response.raise_for_status()
|
||||
return response.text
|
||||
except (httpx.HTTPError, asyncio.TimeoutError) as exc:
|
||||
last_error = exc
|
||||
if attempt == 0:
|
||||
await asyncio.sleep(0.4)
|
||||
raise RuntimeError(f"公告详情页获取失败({gonggao_id})") from last_error
|
||||
|
||||
|
||||
async def sync_all(db, client: httpx.AsyncClient | None = None) -> dict:
|
||||
owns_client = client is None
|
||||
if owns_client:
|
||||
@@ -238,11 +274,23 @@ async def sync_all(db, client: httpx.AsyncClient | None = None) -> dict:
|
||||
added = updated = 0
|
||||
try:
|
||||
first = await fetch_page(client, 1)
|
||||
pages = int(first.get("extra", {}).get("pages") or 1)
|
||||
pages = max(1, int(first.get("extra", {}).get("pages") or 1))
|
||||
page_size = 50
|
||||
pages = max(pages, -(-len(first.get("rows") or []) // page_size))
|
||||
rows = list(first.get("rows") or [])
|
||||
for page in range(2, pages + 1):
|
||||
await asyncio.sleep(0.15)
|
||||
rows.extend((await fetch_page(client, page)).get("rows") or [])
|
||||
details: dict[int, str] = {}
|
||||
for raw in rows:
|
||||
content = str(raw.get("gonggaoContent") or "").strip()
|
||||
if len(content) <= 20 and not raw.get("iswailian"):
|
||||
try:
|
||||
gonggao_id = int(raw["gonggaoId"])
|
||||
await asyncio.sleep(0.15)
|
||||
details[gonggao_id] = await fetch_detail_html(client, gonggao_id)
|
||||
except (KeyError, TypeError, ValueError, RuntimeError):
|
||||
logger.warning("公告详情补抓失败:%s", raw.get("gonggaoId"), exc_info=True)
|
||||
finally:
|
||||
if owns_client:
|
||||
await client.aclose()
|
||||
@@ -256,7 +304,7 @@ async def sync_all(db, client: httpx.AsyncClient | None = None) -> dict:
|
||||
"gonggao_id": gonggao_id,
|
||||
"title": str(raw.get("gonggaoTitle") or "未命名公告"),
|
||||
"companyname": str(raw.get("companyname") or ""),
|
||||
"content_html": str(raw.get("gonggaoContent") or ""),
|
||||
"content_html": details.get(gonggao_id) and extract_detail_html(details[gonggao_id]) or str(raw.get("gonggaoContent") or ""),
|
||||
"create_time": str(raw.get("createTime") or ""),
|
||||
"check_time": str(raw.get("checkTime") or ""),
|
||||
"valid_time": str(raw.get("validTime") or ""),
|
||||
|
||||
Reference in New Issue
Block a user