공공기관2 작업 중. _temp 몽타주(재생성가능)는 제외. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
33 lines
1.5 KiB
Python
33 lines
1.5 KiB
Python
# -*- coding: utf-8 -*-
|
|
# 충주 구버전 사이트맵 트리: div.sitemap_box > h3.h0(대) / li>a.h4(중) / ul.bu>li>a(소)
|
|
import sys,io,json,urllib.request,ssl
|
|
from bs4 import BeautifulSoup
|
|
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
|
|
def get(u):
|
|
req=urllib.request.Request(u,headers={'User-Agent':'Mozilla/5.0'})
|
|
return urllib.request.urlopen(req,timeout=25,context=ctx).read().decode('utf-8','ignore')
|
|
h=get('https://www.chungju.go.kr/www/sitemap.do?key=553')
|
|
s=BeautifulSoup(h,'html.parser')
|
|
tree=[]
|
|
for box in s.select('div.sitemap_box'):
|
|
h3=box.select_one('h3.h0 a, h3 a')
|
|
d1=h3.get_text(strip=True) if h3 else ''
|
|
ul=box.find('ul', recursive=False) or box.find('ul')
|
|
if not ul: continue
|
|
for li in ul.find_all('li', recursive=False):
|
|
a4=li.find('a', class_='h4') or li.find('a', recursive=False)
|
|
if not a4: continue
|
|
d2=a4.get_text(strip=True)
|
|
bu=li.find('ul', class_='bu') or li.find('ul', recursive=False)
|
|
if bu:
|
|
for sub in bu.find_all('li', recursive=False):
|
|
a=sub.find('a')
|
|
if not a: continue
|
|
tree.append([d1,d2,a.get_text(strip=True),'',a.get('href')])
|
|
else:
|
|
tree.append([d1,d2,'','',a4.get('href')])
|
|
json.dump(tree,open('_cz_tree.json','w',encoding='utf-8'),ensure_ascii=False)
|
|
print('충주 트리노드',len(tree))
|
|
for t in tree[:6]: print(' ',t[0],'>',t[1],'>',t[2],'|',(t[4] or '')[:45])
|