공공기관2 작업 중. _temp 몽타주(재생성가능)는 제외. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
35 lines
1.6 KiB
Python
35 lines
1.6 KiB
Python
# -*- coding: utf-8 -*-
|
|
# 증평 트리: ul.depth1_ul>li.th1>a.th1_lnk(d1) / ul.depth2_ul>li>a(d2) / ul.depth3_ul>li>a.lnk_2th(d3) [/depth4_ul]
|
|
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.jp.go.kr/kor/sitemap_11.do')
|
|
s=BeautifulSoup(h,'html.parser')
|
|
def txt(a): return a.get_text(strip=True) if a else ''
|
|
tree=[]
|
|
for li1 in s.select('ul.depth1_ul > li.th1'):
|
|
a1=li1.select_one('a.th1_lnk'); d1=txt(a1)
|
|
d2s=li1.select('ul.depth2_ul > li')
|
|
if not d2s:
|
|
tree.append([d1,'','','',a1.get('href')]); continue
|
|
for li2 in d2s:
|
|
a2=li2.find('a', recursive=False) or li2.find('a'); d2=txt(a2)
|
|
d3s=li2.select('ul.depth3_ul > li')
|
|
if not d3s:
|
|
tree.append([d1,d2,'','',a2.get('href')]); continue
|
|
for li3 in d3s:
|
|
a3=li3.find('a'); d3=txt(a3)
|
|
d4s=li3.select('ul.depth4_ul > li')
|
|
if not d4s:
|
|
tree.append([d1,d2,d3,'',a3.get('href')])
|
|
else:
|
|
for li4 in d4s:
|
|
a4=li4.find('a'); tree.append([d1,d2,d3,txt(a4),a4.get('href')])
|
|
json.dump(tree,open('_jp_tree.json','w',encoding='utf-8'),ensure_ascii=False)
|
|
print('증평 트리노드',len(tree))
|
|
for t in tree[:5]: print(' ',t[0],'>',t[1],'>',t[2],'>',t[3],'|',(t[4] or '')[:40])
|