# -*- coding: utf-8 -*- import sys,io,json sys.stdout=io.TextIOWrapper(sys.stdout.buffer,encoding='utf-8') from playwright.sync_api import sync_playwright with sync_playwright() as p: br=p.chromium.launch(headless=True) ctx=br.new_context(ignore_https_errors=True,viewport={'width':1920,'height':1080}, user_agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/120 Safari/537.36') pg=ctx.new_page() pg.goto("https://www.nypi.re.kr/",wait_until='networkidle',timeout=40000); pg.wait_for_timeout(2000) # hover 모든 1depth try: tops=pg.query_selector_all('#gnb>ul>li, .gnb>ul>li, nav>ul>li, header li') for e in tops[:10]: try: e.hover(timeout=400); pg.wait_for_timeout(150) except: pass except: pass r=pg.evaluate(r"""()=>{ function txt(a){return (a.textContent||'').trim().replace(/\s+/g,' ');} // 가장 a많은 ul 컨테이너 let best=null,bn=0; document.querySelectorAll('ul').forEach(u=>{const n=u.querySelectorAll('a').length; if(n>bn&&n<200){bn=n;best=u;}}); let tree=[]; if(best){ const top=best.closest('nav')||best.parentElement||best; top.querySelectorAll(':scope > ul > li, :scope > li').forEach(li=>{ const a=li.querySelector('a'); if(!a)return; const subs=[...li.querySelectorAll('ul a')].map(x=>txt(x)).filter(Boolean); tree.push({d1:txt(a),subs:subs.slice(0,30)}); }); } return tree.slice(0,15); }""") print(json.dumps(r,ensure_ascii=False)[:1500]) br.close()