import sys, io, json
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
import openpyxl, urllib.request, ssl, re
from concurrent.futures import ThreadPoolExecutor

P = r'D:\01.프로젝트\DB수집\작업파일\광역_사이트맵\충청남도\10.아산시\충청남도_아산시.xlsx'
wb = openpyxl.load_workbook(P); ws = wb.active
ctx = ssl.create_default_context(); ctx.check_hostname=False; ctx.verify_mode=ssl.CERT_NONE
hdr={'User-Agent':'Mozilla/5.0'}
def fetch(url):
    req=urllib.request.Request(url, headers=hdr)
    with urllib.request.urlopen(req, context=ctx, timeout=25) as r:
        return r.read().decode('utf-8','replace')

targets=[]
for r in range(37, ws.max_row+1):
    k=ws[f'K{r}'].value; l=ws[f'L{r}'].value; o=ws[f'O{r}'].value
    if not k or 'asan.go.kr' not in str(k) or l=='사이트': continue
    targets.append((r,str(k),l,o))

def work(t):
    r,k,l,o=t
    try: html=fetch(k)
    except Exception as e: return (r,k,l,o,'ERR',None,None)
    mt=re.search(r'img_opentype_(\d)\.gif', html)
    otype=mt.group(1) if mt else None
    # broad: any <ul>/<div> nav containing >=3 anchors with &pg= and text labels (not pure numbers)
    tabs=None
    for um in re.finditer(r'<(ul|div)([^>]*)>(.*?)</\1>', html, re.S):
        attrs=um.group(2); body=um.group(3)
        anchors=re.findall(r'<a[^>]*href="([^"]*&pg=\d+[^"]*)"[^>]*>(.*?)</a>', body, re.S)
        labels=[(h, re.sub(r'<[^>]+>','',t).strip()) for h,t in anchors]
        named=[x for x in labels if x[1] and not x[1].isdigit()]
        if len(named)>=3:
            cls=re.search(r'class="([^"]*)"', attrs)
            tabs={'cls':cls.group(1) if cls else '', 'items':named}
            break
    return (r,k,l,o,None,otype,tabs)

res=[]
with ThreadPoolExecutor(max_workers=10) as ex:
    for out in ex.map(work, targets): res.append(out)

print('=== in-page pg= tab navs (broad) ===')
seen=set()
for r,k,l,o,err,otype,tabs in sorted(res):
    if tabs:
        # base no
        base=re.search(r'no=(\d+)', k)
        print(f'r{r}: cls={tabs["cls"]!r} n={len(tabs["items"])} L={l} no={base.group(1) if base else "?"}  K={k}')

print('\n=== any opentype marks (should be only the ones already typed posts) ===')
for r,k,l,o,err,otype,tabs in sorted(res):
    if otype:
        print(f'r{r}: TYPE={otype} O={o!r} L={l} K={k}')

print('\nerrors:', [r for r,k,l,o,err,otype,tabs in res if err])
