import requests, io, re
from bs4 import BeautifulSoup
hd={"User-Agent":"Mozilla/5.0"}
BASE="https://www.kofpi.or.kr"
out=io.open("_counts_out.txt","w",encoding="utf-8")
def cnt(u):
    # try last page then count items, or find total via number column
    r=requests.get(BASE+u,headers=hd,timeout=25); r.encoding=r.apparent_encoding
    s=BeautifulSoup(r.content,"html.parser")
    txt=s.get_text(" ",strip=True)
    m=re.search(r"(?:총|전체)\s*(?:게시물)?\s*[:：]?\s*([\d,]+)\s*(?:건|개)",txt)
    tot=m.group(1) if m else None
    # board number column (sequential) max -> total
    nums=[]
    for td in s.select("table tbody tr td.num, table tbody tr td:first-child, .num, td.no"):
        t=td.get_text(strip=True)
        if t.isdigit(): nums.append(int(t))
    # gallery items
    gal=len(s.select(".gallery li, .photo_list li, ul.list_gallery li, .bbs_gallery li, li.gallery"))
    return tot, (max(nums) if nums else None), gal
for nm,u in [
 ("임산물레시피","/info/imupStory/forestStory_02.do"),
 ("세계목재300종","/info/imupStory/wood_03.do"),
 ("해외시장보고서","/info/exportInfo/exportData_02.do"),
 ("연구보고서수출","/info/exportInfo/exportData_03.do"),
 ("통계자료수출","/info/exportInfo/exportData_04.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"),
 ("관련양식기술","/info/tech/techComm_03_01.do"),
 ("목재제품규격품질검사","/service/introduceInfo_04.do"),
 ("KS인증","/service/introduceInfo_11.do"),
 ("임업기계장비인증","/service/introduceInfo_34.do"),
 ("조달물자검사","/service/introduceInfo_48.do"),
 ("수출업체","/info/exportInfo/exportSite_02_01.do"),
 ("관련사이트수출","/info/exportInfo/exportSite_02_02_01.do"),
 ("기관별지원사업","/info/exportInfo/govSupport_03_01.do"),
 ("내게맞는지원사업","/info/exportInfo/govSupport_04_01_01.do"),
]:
    try:
        tot,mx,gal=cnt(u)
        out.write(f"{nm} {u}  total={tot} maxnum={mx} gallery={gal}\n")
    except Exception as e:
        out.write(f"{nm} {u} ERR {e}\n")
out.close(); print("done")
