DB_JOB/작업파일/공공기관2/15.한국청소년활동진흥원/_scan.py
hehihoho3 b0acd1e705 백업: 셀병합 정상화(D~J)+KDHC 옵션2/행높이17, 오늘(06-21) 제출 17곳 검수·압축
- 공공기관2/3 작업본 + 오늘 제출 17곳 D~J 카테고리 셀병합 정상화
- 한국지역난방공사 옵션2(고아셀 F98 수정)+전행 높이17
- 제출_프리랜서2_2026-06-21.zip 생성(17개 xlsx, 2,468행)

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

46 lines
2.2 KiB
Python

# -*- coding: utf-8 -*-
import openpyxl,sys,urllib.request,ssl,re,bs4,time
sys.stdout.reconfigure(encoding='utf-8')
ctx=ssl.create_default_context(); ctx.check_hostname=False; ctx.verify_mode=ssl.CERT_NONE
def fetch(u,tries=3):
for i in range(tries):
try:
req=urllib.request.Request(u,headers={'User-Agent':'Mozilla/5.0'})
return urllib.request.urlopen(req,timeout=20,context=ctx).read()
except Exception as e:
if i==tries-1: raise
time.sleep(2)
def maxnum(soup):
nums=[]
for el in soup.select('.b-left-n, td.b-left-n, th.b-left-n'):
t=el.get_text(strip=True)
if t.isdigit(): nums.append(int(t))
return max(nums) if nums else None
wb=openpyxl.load_workbook('한국청소년활동진흥원.xlsx'); ws=wb.active
out=[]
for r in range(3,ws.max_row+1):
url=ws.cell(r,11).value
curL=ws.cell(r,12).value
if not url or not str(url).startswith('http'):
out.append(f'{r}\tEMPTY/NOURL'); continue
if 'kywa.or.kr' not in url or url.startswith('https://booking'):
out.append(f'{r}\tEXT\t{url}'); continue
try:
h=fetch(url); soup=bs4.BeautifulSoup(h,'html.parser')
tc=[(a.get_text(strip=True),a.get('href','')) for a in soup.select('ul.tab-clear li a.tap-setting, ul.tab-clear li a')]
has_all=any(t[0]=='전체' for t in tc)
is_content_tab = tc and not has_all and len(tc)>=2 and all('.jsp' in (t[1] or '') for t in tc)
mx=maxnum(soup)
gp=[int(x) for x in re.findall(r'go_page\((\d+)\)',h.decode('utf-8','ignore'))]
lastpg=max(gp) if gp else None
board = (mx is not None) or has_all or (lastpg and lastpg>1)
kind='CONTENT_TAB' if is_content_tab else ('CAT_BOARD' if (has_all or board) else 'PAGE')
info=f'{r}\t{kind}\tcurL={curL}\tM(maxnum)={mx}\tlastpg={lastpg}\ttabs={len(tc)}'
if is_content_tab: info+='\tCONTENT:'+' | '.join(f'{t[0]}->{t[1]}' for t in tc)
elif tc: info+='\tCATS:'+'/'.join(t[0] for t in tc)
out.append(info+'\t'+url.split('/')[-1][:30])
except Exception as e:
out.append(f'{r}\tERR\t{str(e)[:40]}\t{url[:50]}')
open('_scan_out.txt','w',encoding='utf-8').write('\n'.join(out))
print('\n'.join(out))