DB_JOB/작업파일/완료/광역_사이트맵/충청북도/_blank_apply.py
hehihoho3 df16c98366 백업: DB수집 전체 스냅샷 (공공기관2 정리 전)
공공기관2 작업 중. _temp 몽타주(재생성가능)는 제외.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-18 18:15:40 +09:00

40 lines
1.5 KiB
Python

# -*- coding: utf-8 -*-
# 새창열림(_blank) → 사이트 소급적용: python _blank_apply.py <sitemap_url> <host> <excel>
import sys,io,urllib.request,ssl,re
from bs4 import BeautifulSoup
import openpyxl
sys.stdout=io.TextIOWrapper(sys.stdout.buffer,encoding='utf-8')
SM=sys.argv[1]; HOST=sys.argv[2]; F=sys.argv[3]
BASE='https://'+HOST
ctx=ssl.create_default_context(); ctx.check_hostname=False; ctx.verify_mode=ssl.CERT_NONE
try: ctx.set_ciphers('DEFAULT@SECLEVEL=1')
except: pass
def absu(u):
if not u: return ''
if u.startswith('http'): return u
if u.startswith('/'): return BASE+u
return BASE+'/'+u
def norm(u): return absu(u).split('#')[0].rstrip('/')
req=urllib.request.Request(SM,headers={'User-Agent':'Mozilla/5.0'})
h=urllib.request.urlopen(req,timeout=25,context=ctx).read().decode('utf-8','ignore')
s=BeautifulSoup(h,'html.parser')
blank=set()
for a in s.find_all('a'):
tg=(a.get('target') or ''); ti=(a.get('title') or ''); tx=a.get_text(strip=True)
if tg=='_blank' or '새창' in ti or '새창' in tx:
href=a.get('href')
if href and not href.startswith('javascript') and href!='#':
blank.add(norm(href))
print('새창열림 URL',len(blank))
wb=openpyxl.load_workbook(F); ws=wb.active
chg=0
for r in range(3,ws.max_row+1):
u=ws.cell(r,11).value
if not u: continue
if norm(u) in blank and ws.cell(r,12).value!='사이트':
ws.cell(r,12).value='사이트'
for c in (13,14,15,16,17): ws.cell(r,c).value=None
chg+=1
wb.save(F)
print('사이트 정정',chg,'')