"""제천시·증평군 구조 재확인."""
import warnings
import requests
from bs4 import BeautifulSoup

warnings.filterwarnings('ignore')
UA = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
H = {'User-Agent': UA}


def outline(el, d=0, max_lines=70, lines=None):
    if lines is None: lines = []
    if len(lines) >= max_lines: return lines
    name = el.name
    cls = ' '.join(el.get('class', []))
    eid = el.get('id', '')
    lbl = name
    if eid: lbl += f'#{eid}'
    if cls: lbl += '.' + cls.replace(' ', '.')
    if name == 'a':
        t = el.get_text(strip=True)[:40]
        h = el.get('href', '')[:80]
        lines.append('  ' * d + f'{lbl} "{t}" → {h}')
    else:
        lines.append('  ' * d + lbl)
    for c in el.find_all(recursive=False):
        if c.name in ('script', 'style'): continue
        outline(c, d+1, max_lines, lines)
        if len(lines) >= max_lines: return lines
    return lines


# 제천시
print('='*70)
print('제천시 — div.depth1 outline')
print('='*70)
r = requests.get('https://www.jecheon.go.kr/www/sitemap.do?key=553', headers=H, timeout=15, verify=False)
r.encoding = r.apparent_encoding
soup = BeautifulSoup(r.text, 'html.parser')
for sel in ['div.depth1', 'div.depth.depth1', '#sitemap']:
    el = soup.select_one(sel)
    if el:
        print(f'\n>>> {sel} (a={len(el.find_all("a"))}, classes={el.get("class")})')
        for line in outline(el, max_lines=50):
            print('  ', line)
        break


# 증평군 - ul.depth1_ul outline
print('\n' + '='*70)
print('증평군 — ul.depth1_ul outline')
print('='*70)
r = requests.get('https://www.jp.go.kr/kor/sitemap_11.do', headers=H, timeout=15, verify=False)
r.encoding = r.apparent_encoding
soup = BeautifulSoup(r.text, 'html.parser')
el = soup.select_one('ul.depth1_ul')
if el:
    print(f'  (a={len(el.find_all("a"))}, classes={el.get("class")})')
    for line in outline(el, max_lines=60):
        print('  ', line)
