import sys, io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
import urllib.request, ssl, re
ctx=ssl.create_default_context(); ctx.check_hostname=False; ctx.verify_mode=ssl.CERT_NONE
hdr={'User-Agent':'Mozilla/5.0'}
def fetch(url):
    req=urllib.request.Request(url, headers=hdr)
    with urllib.request.urlopen(req, context=ctx, timeout=25) as r:
        return r.read().decode('utf-8','replace')

def content_imgs(html):
    # extract main content area #contents or .contents and find non-decorative imgs
    m=re.search(r'id="contents"(.*?)(?:</main>|<footer|id="footer)', html, re.S) or \
      re.search(r'class="[^"]*contents[^"]*"(.*?)(?:</main>|<footer|id="footer)', html, re.S)
    area=m.group(1) if m else html
    imgs=re.findall(r'<img[^>]*src="([^"]*)"[^>]*>', area)
    deco=re.compile(r'move\.png|no[-_]?img|blank|spacer|icon|btn_|bullet|/common/|opentype', re.I)
    real=[s for s in imgs if not deco.search(s)]
    return imgs, real

for pg in range(10):
    url=f'https://www.asan.go.kr/main/cms/?no=55&pg={pg}'
    html=fetch(url)
    t=re.search(r'<h[1-4][^>]*class="[^"]*(?:tit|title)[^"]*"[^>]*>(.*?)</h', html, re.S)
    title=re.sub(r'<[^>]+>','',t.group(1)).strip() if t else ''
    allimg, real = content_imgs(html)
    ot=re.search(r'img_opentype_(\d)', html)
    # detect tables, embeds, video
    has_table='<table' in html
    has_video=bool(re.search(r'<video|youtube|<iframe', html, re.I))
    print(f'pg={pg} title={title!r} opentype={ot.group(1) if ot else None} realimg={len(real)} table={has_table} video={has_video}')
    for s in real[:6]:
        print('      img:', s)

print('\n===== no=365 search-list inspect =====')
html=fetch('https://www.asan.go.kr/main/cms/?no=365')
m=re.search(r'<[^>]*class="[^"]*search-list[^"]*"[^>]*>(.*?)</(?:ul|div)>', html, re.S)
if m:
    seg=m.group(1)
    print(re.sub(r'\s+',' ', seg)[:1500])
