import sys, io, re, ssl, json, urllib.request sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8') ctx = ssl.create_default_context(); ctx.check_hostname=False; ctx.verify_mode=ssl.CERT_NONE BASE='https://www.jangsu.go.kr/index.jangsu?menuCd=' def fetch(cd, tries=3): for i in range(tries): try: return urllib.request.urlopen(urllib.request.Request(BASE+cd, headers={'User-Agent':'Mozilla/5.0'}), context=ctx, timeout=40).read().decode('utf-8','replace') except Exception as e: if i==tries-1: raise # 대분류/중분류 라벨 — location nav(현재 위치) 또는 breadcrumb에서 def labels(cd): h=fetch(cd) # breadcrumb: class="location" 안의 텍스트들 loc=re.search(r'class="(?:location|path|breadcrumb)[^"]*"(.*?)(?:ul|div|nav)>', h, re.S) crumbs=[] if loc: crumbs=[re.sub(r'<[^>]+>','',x).strip() for x in re.findall(r'<(?:li|a|span)[^>]*>(.*?)(?:li|a|span)>', loc.group(1))] crumbs=[c for c in crumbs if c and c not in ('홈','HOME','home')] # title t=re.search(r'