import requests, io, re from bs4 import BeautifulSoup hd={"User-Agent":"Mozilla/5.0"} BASE="https://www.kofpi.or.kr" out=io.open("_invest_out.txt","w",encoding="utf-8") def fetch(u): r=requests.get(u,headers=hd,timeout=25); r.encoding=r.apparent_encoding return BeautifulSoup(r.content,"html.parser"), r # (a) ALL tab widgets on the 주요사업 ambiguous pages for u in ["/service/introduceInfo_01.do","/service/introduceInfo_47.do","/service/introduceInfo_20.do","/service/certInfo_02.do","/service/edu_01.do"]: s,_=fetch(BASE+u) out.write(f"\n=== {u} ALL tab-like UL/DIV ===\n") for w in s.select("div[class*=tab], ul[class*=tab]"): cls=w.get("class") lis=w.find_all("li", recursive=True) labs=[(li.find('a').get_text(' ',strip=True)[:18] if li.find('a') else li.get_text(' ',strip=True)[:18], (li.find('a').get('href') if li.find('a') else None)) for li in lis][:12] if labs: out.write(f" {w.name}.{cls}: {labs}\n") # (b) board counts: find total/lastpage for paging boards boards={ "임산물정보":"/info/imupStory/forestStory_01.do","임산물레시피":"/info/imupStory/forestStory_02.do", "우리목재":"/info/imupStory/wood_02.do", "공모중인사업":"/info/exportInfo/govSupport_02.do", "통합자료실(수출)":"/info/exportInfo/exportData_01.do","해외시장보고서":"/info/exportInfo/exportData_02.do", "연구보고서(수출)":"/info/exportInfo/exportData_03.do","발굴업체홍보":"/info/exportInfo/exportData_05.do","임업기계장비(수출)":"/info/exportInfo/exportData_06.do", "기술이전현황":"/info/tech/techComm_01_02.do","사업화성공사례":"/info/tech/techComm_01_03.do","보유특허정보":"/info/tech/techComm_01_01.do","기술자료집":"/info/tech_06.do", } out.write("\n\n=== BOARD COUNTS ===\n") for nm,u in boards.items(): s,_=fetch(BASE+u) txt=s.get_text(" ",strip=True) tot=None for pat in [r"총\s*([\d,]+)\s*건",r"전체\s*([\d,]+)",r"총\s*게시(?:물|글)?\s*[::]?\s*([\d,]+)",r"게시물\s*[::]?\s*([\d,]+)\s*건"]: m=re.search(pat,txt) if m: tot=m.group(1); break # last page number from pagination links pglinks=[a.get('href','') for a in s.select("[class*=paging] a, [class*=page] a, .pagination a")] pgnums=[] for a in s.select("[class*=paging] a, .pagination a, [class*=page] a"): t=a.get_text(strip=True) if t.isdigit(): pgnums.append(int(t)) # number column max in list nums=[] for td in s.select("table tbody tr td, ul.board_list li, .board_list .num"): t=td.get_text(strip=True) if t.isdigit(): nums.append(int(t)) out.write(f" {nm} {u}\n total_text={tot} maxpage={max(pgnums) if pgnums else None} maxnum={max(nums) if nums else None}\n") out.close() print("done")