DB_JOB/작업파일/공공기관2/6.한국중부발전/_temp_firstpost.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

28 lines
1.6 KiB
Python

# -*- coding: utf-8 -*-
import ssl,urllib.request,re
from urllib.parse import urljoin
from bs4 import BeautifulSoup
ctx=ssl.create_default_context();ctx.check_hostname=False;ctx.verify_mode=ssl.CERT_NONE
HDR={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/124 Safari/537.36'}
def fetch(u): return urllib.request.urlopen(urllib.request.Request(u,headers=HDR),timeout=25,context=ctx).read()
UI=re.compile(r'banner|/common/|icon|btn|logo|bullet|sprite|blank|no_img|arrow|/dot|/bg|popup|/sns',re.I)
B='https://www.komipo.co.kr'
for lab,u in [('전사 캠페인','/esg/board/BRD_000096/boardMain.do?mnCd=ESG03030203'),
('인권경영 자료공유','/esg/board/BRD_000097/boardMain.do?mnCd=ESG03030303')]:
soup=BeautifulSoup(fetch(B+u),'html.parser')
# find first post link in list
a=None
for cand in soup.select('#content a[href*=boardView], #content a[href*=Idx], #content td a, #content a[onclick]'):
t=cand.get_text(strip=True)
if t and len(t)>2: a=cand; break
print('===',lab,'first link:', a.get('href','')[:60] if a else 'NONE', a.get('onclick','')[:60] if a and a.get('onclick') else '')
# try follow href
href=a.get('href','') if a else ''
if href and href!='#' and 'javascript' not in href:
try:
s2=BeautifulSoup(fetch(urljoin(B+u,href)),'html.parser')
cont=s2.select_one('#content') or s2
imgs=[im.get('src') for im in cont.find_all('img') if 'getImage' in im.get('src','') and not UI.search(im.get('src',''))]
print(' 본문 content imgs:',len(imgs), imgs[:3])
except Exception as e: print(' follow err',str(e)[:40])