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 h = urllib.request.urlopen(urllib.request.Request('https://www.jangsu.go.kr/index.jangsu', headers={'User-Agent':'Mozilla/5.0'}), context=ctx, timeout=50).read().decode('utf-8','replace') # 중분류 라벨: 106XXX000000 링크 안/근처의 span 텍스트 (img/icon 사이 허용) mid = {} for m in re.finditer(r'menuCd=DOM_000000(\d{6})000000"[^>]*>(.*?)', h, re.S): code = m.group(1) # 대3+중3 inner = m.group(2) sp = re.findall(r']*>([^<]+)', inner) lab = next((s.strip() for s in sp if s.strip()), '') if code[3:] != '000' and lab and code not in mid: mid[code] = lab tree = json.load(open(r'D:\01.프로젝트\DB수집\작업파일\_스크립트\_jangsu_tree.json', encoding='utf-8')) old = dict(tree['중']) # 병합: 새로 찾은 라벨로 보강 added = {} for code, lab in mid.items(): if code not in old or not old.get(code): added[code] = lab old.update(mid) tree['중'] = old json.dump(tree, open(r'D:\01.프로젝트\DB수집\작업파일\_스크립트\_jangsu_tree.json','w',encoding='utf-8'), ensure_ascii=False, indent=1) print('중분류 총:', len(old)) print('새로 채운 중분류:', len(added)) for k,v in sorted(added.items()): print(f' {k} = {v}') # 분야별정보(106) 중분류 확인 print('\n분야별정보(106) 중분류:') for k,v in sorted(old.items()): if k.startswith('106'): print(f' {k} = {v}')