"""진천군: nav#gnb 더 깊이 들어가서 실제 메뉴 찾기. + 보은군 한번 더."""
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}


def fetch(url, timeout=20):
    try:
        r = requests.get(url, headers=H, timeout=timeout, verify=False, allow_redirects=True)
        r.encoding = r.apparent_encoding
        return r.status_code, r.url, r.text
    except Exception as e:
        return 0, str(e), ''


# 진천군 - find all elements with menu-like classes
print('='*70)
print('진천군 — find menu containers')
print('='*70)
_, _, html = fetch('https://www.jincheon.go.kr/home/sub.do?menukey=445')
soup = BeautifulSoup(html, 'html.parser')
# Look for elements with 'depth' / 'gnb' / 'menu' / 'sitemap' in classes
print('All elements with 100+ anchors and depth/menu/sitemap in class:')
for el in soup.find_all(['div', 'ul', 'nav', 'section']):
    cls = ' '.join(el.get('class', []))
    eid = el.get('id', '')
    ac = len(el.find_all('a'))
    if ac >= 100 and re.search(r'depth|menu|sitemap|allmenu|total|all-menu|gnb', cls + ' ' + eid, re.I):
        print(f'  {el.name}#{eid}.{cls[:60]} a={ac}')

# Find all .depth* classes
print('\n.depth* classes with anchors:')
for el in soup.select('[class*=depth]'):
    cls = ' '.join(el.get('class', []))
    ac = len(el.find_all('a'))
    if ac >= 50:
        print(f'  {el.name}.{cls[:80]} a={ac}')


# 보은군 한번 더
print('\n' + '='*70)
print('보은군 retry')
print('='*70)
import time
time.sleep(1)
code, real, html = fetch('https://www.boeun.go.kr/www/index.do')
print(f'  {code} {real[:100]}')

if code == 200:
    print('  성공! 사이트맵 후보 탐색')
    from urllib.parse import urljoin
    s2 = BeautifulSoup(html, 'html.parser')
    for a in s2.find_all('a', href=True)[:200]:
        txt = a.get_text(strip=True)
        if '사이트맵' in txt or '전체메뉴' in txt or '누리집 지도' in txt:
            print(f'    "{txt}" → {urljoin(real, a["href"])}')
