from playwright.sync_api import sync_playwright
targets={
 '사회공헌위원회':"https://www.kdhc.co.kr/kdhc/bbs/B0000008/list.do?menuNo=200106",
 '시민열냉방시설기준':"https://www.kdhc.co.kr/kdhc/cstmrCnter/arclngFclty/list.do?fcltyParntsSe=02&menuNo=200276",
 '안전경영방침':"https://www.kdhc.co.kr/kdhc/bbs/B0000030/list.do?menuNo=200091",
 '한난사보20-23':"https://www.kdhc.co.kr/kdhc/prCnter/cyclb/list.do?prTargetSe=2&menuNo=200127",
}
import io
with sync_playwright() as p:
    b=p.chromium.launch(); pg=b.new_page()
    res={}
    for nm,u in targets.items():
        pg.goto(u,wait_until='networkidle',timeout=30000)
        # try main content selectors
        txt=''
        for sel in ['#contents','#content','.sub_contents','#container','.contents']:
            el=pg.query_selector(sel)
            if el:
                txt=el.inner_text(); break
        # strip menu noise: take last 400 chars of content region or search keywords
        res[nm]=txt
    b.close()
import re
for nm,txt in res.items():
    has_empty='게시물이 없습니다' in txt or '등록된' in txt and '없' in txt
    # find the content after breadcrumb 'Home'
    print('==',nm,'empty_msg=',('게시물이 없습니다' in txt))
    # print middle/end portion
    io.open(f'_ct_{nm}.txt','w',encoding='utf-8').write(txt)
    print('   tail:', txt[-200:].replace(chr(10),' '))
