"""5차: 부안/완주/군산/순창 인라인 메가메뉴 중첩 구조 정밀 덤프.""" import re, 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' def fetch(url, t=20): r = requests.get(url, timeout=t, verify=False, headers={'User-Agent': UA}) meta = re.search(rb']*charset=["\']?\s*([\w-]+)', r.content[:4096], re.I) r.encoding = meta.group(1).decode('ascii','ignore') if meta else r.apparent_encoding return r def tree(el, depth=0, maxd=4, maxchild=6): if el is None or depth > maxd: return kids = el.find_all(recursive=False) for i, c in enumerate(kids): if i >= maxchild and depth >= 1: print(' '*(depth+1) + '...'); break cls = '.'.join(c.get('class', [])) a = c.find('a', recursive=False) atxt = a.get_text().strip()[:18] if a else '' ah = (a.get('href') or '')[:55] if a else '' print(' '*(depth+1) + f'{c.name}.{cls}' + (f' a={atxt!r} {ah}' if atxt else '')) tree(c, depth+1, maxd, maxchild) print('############ 부안 nav#onmenu ############') soup = BeautifulSoup(fetch('https://www.buan.go.kr/index.buan?contentsSid=1').text, 'html.parser') nav = soup.select_one('nav#onmenu') # 첫 1~2개 top li만 if nav: top = nav.find('ul') print('nav>ul 첫 li 2개:') for li in (top.find_all('li', recursive=False)[:2] if top else []): tree(li, 0, 4, 5) print(' ----') print('\n############ 완주 div.top_menu_wrap ############') soup = BeautifulSoup(fetch('https://www.wanju.go.kr/index.9is').text, 'html.parser') w = soup.select_one('div.top_menu_wrap') tree(w, 0, 3, 4) print('\n############ 군산 첫 allmenubox ############') soup = BeautifulSoup(fetch('https://www.gunsan.go.kr/main').text, 'html.parser') pc = soup.select_one('div#all_pcmenu') if pc: boxes = pc.select('div.allmenubox') print(f'allmenubox 수: {len(boxes)}') b = boxes[0] print('Bmenu(대분류):', repr((b.find('a', class_='Bmenu') or b.find('a')).get_text().strip()[:20])) tree(b, 0, 4, 5) print('\n############ 순창 ul.gnb ############') soup = BeautifulSoup(fetch('https://www.sunchang.go.kr/').text, 'html.parser') g = soup.select_one('ul.gnb') if g: print(f'ul.gnb 직계 li: {len(g.find_all("li", recursive=False))}') for li in g.find_all('li', recursive=False)[:1]: tree(li, 0, 4, 5)