import requests, re, json, sys, time
from bs4 import BeautifulSoup
from urllib.parse import urljoin
sys.stdout.reconfigure(encoding='utf-8')
H={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64)'}
sess=requests.Session(); sess.headers.update(H)
rows=json.load(open(r'작업파일/공공기관2/12.한국철도공사/_cur.json',encoding='utf-8'))

def get(u):
    for _ in range(3):
        try: return sess.get(u,timeout=30)
        except: time.sleep(1.5)
    return None

out={}
for o in rows:
    u=o['K'] or ''
    if 'contents.do' not in u: continue
    r=get(u)
    if not r: out[o['r']]={'err':1}; continue
    s=BeautifulSoup(r.content,'html.parser')
    tl=s.select_one('ul.tab_list')
    tabs=[]
    if tl:
        for li in tl.select('li.tab_item'):
            a=li.find('a',href=True)
            if not a: continue
            sp=a.find('span')
            lab=(sp.get_text(strip=True) if sp else a.get_text(strip=True))
            active='active' in (li.get('class') or [])
            tabs.append({'label':lab,'href':a['href'],'active':active})
    out[o['r']]={'tabs':tabs,'ntab':len(tabs)}
    if len(tabs)>1:
        print(o['r'], o['F'] or o['E'] or o['D'], '=>', [(t['label'],t['href'].split('key=')[-1],'*' if t['active'] else '') for t in tabs])
json.dump(out, open(r'작업파일/공공기관2/12.한국철도공사/_tabscan.json','w',encoding='utf-8'),ensure_ascii=False,indent=0)
print('DONE pages scanned', len(out))
