# -*- coding: utf-8 -*- # 청양 LNB(좌측메뉴) 라이브 추출: 군정소식 섹션 페이지에서 depth2/3 메뉴트리 + 각 링크 href import io,sys,json from playwright.sync_api import sync_playwright sys.stdout=io.TextIOWrapper(sys.stdout.buffer,encoding='utf-8') # 군정소식 대분류 진입(새소식) U='https://www.cheongyang.go.kr/kor/sub04_01_01.do' JS=r'''()=>{ function txt(a){return (a.innerText||a.textContent||'').trim().replace(/\s+/g,' ');} const out=[]; // depth2 = 중분류, depth3 = 소분류 document.querySelectorAll('.lnb_depth2__list, ul.depth2_ul').forEach(d2=>{ d2.querySelectorAll(':scope > li').forEach(li2=>{ const a2=li2.querySelector(':scope > a, :scope > .lnb_depth2__btn, :scope > button'); const name2=a2?txt(a2):''; const kids=[]; li2.querySelectorAll('.lnb_depth3__list li > a, ul.depth3_ul li > a').forEach(a3=>{ kids.push({n:txt(a3), href:a3.getAttribute('href')}); }); out.push({d2:name2, href:(a2?a2.getAttribute('href'):null), kids}); }); }); return out; }''' with sync_playwright() as p: b=p.chromium.launch(); pg=b.new_page(viewport={'width':1400,'height':2400}); pg.set_default_timeout(25000) pg.goto(U,wait_until='networkidle'); pg.wait_for_timeout(1500) print('진입 final:',pg.url) data=pg.evaluate(JS) for g in data: print('\n[중] %s (%s)'%(g['d2'],g['href'])) for k in g['kids']: print(' - %s | %s'%(k['n'],k['href'])) b.close()