# -*- coding: utf-8 -*-
import io, sys, os, re, html, json
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
B = r"D:\work buddy\2026-11-05-14-15-52"
h = open(os.path.join(B,"raw_unc.html"),encoding="utf-8").read()
i = h.find('id="byorgtextcontainer"')
seg = h[i:h.find('id="bynametextcontainer"') if h.find('id="bynametextcontainer"')>0 else len(h)]

ps = re.findall(r'<p class="body"[^>]*>(.*?)</p>', seg, re.S)
print("段落数:", len(ps))

def strip_tags(t):
    t = re.sub(r"<a [^>]*>", "|LINK|", t)
    t = re.sub(r"</a>", "", t)
    t = re.sub(r"<[^>]+>", "", t)
    return html.unescape(t).replace("\xa0"," ").strip()

rows=[]
last_top=None
for p in ps:
    indent = "margin-left" in p
    txt = strip_tags(p)
    if not txt: continue
    # 学位链接 href
    hrefs = re.findall(r'href="([^"]+)"', p)
    url = "https://catalog.unc.edu"+hrefs[0] if hrefs else ""
    if "–" in txt:
        name, deg = txt.split("–",1)
        name=name.strip().strip("•").strip(); deg=deg.strip(" ,")
    else:
        name, deg = txt.strip(), ""
    has_deg = bool(re.search(r"M\.|Ph\.?D\.|Dr\.|D\.|MFA|M\.F\.A\.|MPH|MPP|MA|MS|JD|MD|DDS|PhD", deg))
    if not indent:
        if has_deg:
            college=name
            rows.append((college,name,deg,url,"top"))
        else:
            last_top=name
    else:
        rows.append((last_top or last_top,name,deg,url,"sub"))

for r in rows:
    print(f"{r[4]:4}|{(r[0] or '-')[:30]:30}|{r[1][:42]:42}|{r[2][:26]}")
print("总行:", len(rows))
