File size: 5,641 Bytes
dd55845 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | import asyncio
from typing import List, Dict, Any, Tuple
import re
import time
import os
from bs4 import BeautifulSoup
import requests
from playwright.async_api import async_playwright
from .catalog_schema import Assessment, canonical_id, now_iso
CATALOG_URL = "https://www.shl.com/products/product-catalog/"
KENEXA_URL = "https://www.shl.com/c/global/ibm-kenexa-catalog/"
OUTPUT_DIR = "data"
OUTPUT_FILE = os.path.join(OUTPUT_DIR, "catalog.jsonl")
def ensure_dirs():
os.makedirs(OUTPUT_DIR, exist_ok=True)
def normalize_url(u: str) -> str:
u = u.strip()
if u.startswith("//"):
u = "https:" + u
if u.startswith("/"):
u = "https://www.shl.com" + u
u = re.sub(r"[?#].*$", "", u)
return u
def parse_type(text: str) -> str:
t = text.strip().upper()
if "PERSONALITY" in t or t == "P":
return "P"
if "KNOWLEDGE" in t or "SKILLS" in t or t in {"K", "S", "C", "A", "B"}:
return "K"
return t[:1] if t else ""
def extract_detail_fields(html: str, url: str) -> Dict[str, Any]:
soup = BeautifulSoup(html, "html.parser")
name = ""
h1 = soup.find("h1")
if h1 and h1.text:
name = h1.text.strip()
if not name:
title = soup.find("title")
if title and title.text:
name = re.sub(r"\s+\|\s*SHL.*$", "", title.text.strip())
desc = ""
for el in soup.find_all(text=re.compile(r"Description", re.I)):
parent = el.find_parent()
if parent:
nxt = parent.find_next_sibling()
if nxt and nxt.text:
desc = nxt.text.strip()
break
if not desc:
meta = soup.find("meta", attrs={"name": "description"})
if meta and meta.get("content"):
desc = meta["content"].strip()
ttype = ""
for el in soup.find_all(text=re.compile(r"Test Type", re.I)):
parent = el.find_parent()
if parent:
vals = []
for v in parent.find_all("div"):
tx = v.text.strip()
if tx:
vals.append(tx)
if vals:
ttype = parse_type(", ".join(vals))
break
if not ttype:
for badge in soup.find_all("span"):
tx = badge.text.strip().upper()
if tx in {"K", "P", "S", "C", "A", "B", "E"}:
ttype = parse_type(tx)
break
skills = []
tags = []
for ul in soup.find_all("ul"):
items = [li.text.strip() for li in ul.find_all("li") if li.text.strip()]
for it in items:
if len(it.split()) <= 6:
skills.append(it)
skills = list(dict.fromkeys(skills))[:20]
language = "en"
for el in soup.find_all(text=re.compile(r"Languages", re.I)):
parent = el.find_parent()
if parent:
langs = [d.text.strip() for d in parent.find_all("div") if d.text.strip()]
if langs:
language = "en"
break
return {
"name": name,
"description": desc,
"type": ttype or "",
"skills": skills,
"tags": tags,
"language": language,
"url": normalize_url(url),
}
async def collect_catalog_links() -> List[str]:
links = []
try:
async with async_playwright() as p:
browser = await p.chromium.launch(headless=True)
page = await browser.new_page()
await page.goto(CATALOG_URL, wait_until="networkidle")
for _ in range(50):
await page.keyboard.press("End")
await asyncio.sleep(0.2)
html = await page.content()
await browser.close()
soup = BeautifulSoup(html, "html.parser")
for a in soup.find_all("a", href=True):
href = a["href"]
if "/products/product-catalog/view/" in href:
links.append(normalize_url(href))
except Exception:
pass
try:
r = requests.get(KENEXA_URL, headers={"User-Agent": "Mozilla/5.0"}, timeout=30)
soup2 = BeautifulSoup(r.text, "html.parser")
for a in soup2.find_all("a", href=True):
href = a["href"]
if "/c/global/ibm-kenexa-catalog/view/" in href:
links.append(normalize_url(href))
except Exception:
pass
links = list(dict.fromkeys(links))
return links
def fetch(url: str) -> Tuple[int, str]:
hdrs = {"User-Agent": "Mozilla/5.0"}
r = requests.get(url, headers=hdrs, timeout=30)
return r.status_code, r.text
def scrape() -> List[Assessment]:
ensure_dirs()
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
links = loop.run_until_complete(collect_catalog_links())
loop.close()
items = []
for u in links:
code, body = fetch(u)
if code != 200:
continue
data = extract_detail_fields(body, u)
aid = canonical_id(data["url"])
item = Assessment(
id=aid,
name=data["name"],
url=data["url"],
type=data["type"],
description=data["description"],
skills=data["skills"],
tags=data["tags"],
language=data["language"],
scraped_at=now_iso(),
)
items.append(item)
return items
def persist(items: List[Assessment]) -> None:
ensure_dirs()
with open(OUTPUT_FILE, "w", encoding="utf-8") as f:
for it in items:
f.write(it.to_json() + "\n")
def run() -> Dict[str, Any]:
items = scrape()
persist(items)
return {"count": len(items), "output": OUTPUT_FILE}
|