# -*- coding: utf-8 -*-
import openpyxl, time, json, ssl, urllib.request, sys
from bs4 import BeautifulSoup
ctx=ssl.create_default_context();ctx.check_hostname=False;ctx.verify_mode=ssl.CERT_NONE
HDR={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/124 Safari/537.36'}
def fetch(u): return urllib.request.urlopen(urllib.request.Request(u,headers=HDR),timeout=25,context=ctx).read()

wb=openpyxl.load_workbook('한국중부발전.xlsx');ws=wb.active
# find start row = 서울발전본부 사업장소개 content/31
start=None
for r in range(3,ws.max_row+1):
    k=ws.cell(r,11).value
    if k and '/content/31/' in str(k): start=r; break
print('start row',start,file=sys.stderr)
res={}
for r in range(start,ws.max_row+1):
    k=ws.cell(r,11).value
    if not k or 'komipo.co.kr' not in str(k) or '/content/' not in str(k): continue
    try: soup=BeautifulSoup(fetch(str(k)),'html.parser')
    except Exception as e: print('err',r,str(e)[:40],file=sys.stderr); time.sleep(0.4); continue
    # in-page tabs: ul class has 'tab' (not tab_link), li>a with data-tab or href starting '#'
    for ul in soup.find_all('ul'):
        cls=' '.join(ul.get('class') or [])
        if 'tab' not in cls or 'tab_link' in cls: continue
        tabs=[]
        for li in ul.find_all('li', recursive=False):
            a=li.find('a')
            if not a: continue
            href=a.get('href','') or ''
            dt=a.get('data-tab')
            if dt or href.startswith('#'):
                tabs.append(a.get('data-name') or a.get_text(strip=True))
        if len(tabs)>=2:
            res[r]={'b':ws.cell(r,2).value,'leaf':ws.cell(r,6).value or ws.cell(r,7).value or ws.cell(r,5).value,
                    'cls':cls,'tabs':tabs,'curM':ws.cell(r,13).value,'L':ws.cell(r,12).value,'url':str(k)}
    time.sleep(0.6)
json.dump(res,open('inpage.json','w',encoding='utf-8'),ensure_ascii=False,indent=1)
for r,v in sorted(res.items(),key=lambda x:int(x[0])):
    print(f"r{r} b{v['b']} L={v['L']} M{v['curM']}->({len(v['tabs'])}) [{v['cls']}] tabs={v['tabs']}  {v['url'][:46]}")
print('인페이지탭 페이지수:',len(res))
