"""Look at raw HTML around h4.site01 in 보령시 to find category name."""
import re
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}

for name, url in [('보령시', 'https://www.brcn.go.kr/kor/sitemap_11.do'),
                  ('서천군', 'https://www.seocheon.go.kr/kor/sitemap_11.do')]:
    print(f'\n=== {name} === {url}')
    r = requests.get(url, headers=H, timeout=15, verify=False, allow_redirects=True)
    r.encoding = r.apparent_encoding
    html = r.text
    # Find h4.siteNN occurrences
    for m in re.finditer(r'<h4[^>]*site\d+[^>]*>([\s\S]*?)</h4>', html):
        inner = re.sub(r'\s+', ' ', m.group(1)).strip()[:200]
        full = re.sub(r'\s+', ' ', m.group(0)).strip()[:200]
        print(f'  h4: {full}')
    # Find gnb anchors
    soup = BeautifulSoup(html, 'html.parser')
    gnb = soup.select_one('#gnb, #tm, nav#topmenu, nav.gnb, .gnb_wrap, .menu_wrap')
    if gnb:
        a_top = [a.get_text(strip=True) for a in gnb.select('> ul > li > a') or gnb.select('ul > li > a.th_1st, ul > li > a.depth1_ti, ul > li > a.first, ul > li > a')]
        print(f'  GNB top items: {a_top[:10]}')
    # Find the top-level menu anchors (any way)
    print('  Possible main category anchors:')
    for cls_pattern in ['ov', 'th_1st', 'depth1_ti', 'first', 'gnb-main-trigger']:
        for a in soup.find_all('a', class_=cls_pattern):
            t = a.get_text(strip=True)
            if t and len(t) < 30:
                print(f'    .{cls_pattern}: "{t}"')
        break
