import subprocess, time, os, re, html
OUT="tmp/drf_말소폐쇄"; os.makedirs(OUT,exist_ok=True)
seqs=[("P01","95423"),("P02","189033"),("P03","95517"),("P04","187758"),("P05","185818"),
("P06","196829"),("P07","172031"),("P08","207487"),("P09","601587"),
("P10","104519"),("P11","198774"),("P12","106809")]
def cd(s):
s=re.sub(r'', r'\1', s, flags=re.S)
return html.unescape(re.sub(r'<[^>]+>','',s)).strip()
for i,(tag,seq) in enumerate(seqs):
path=f"{OUT}/prec_{tag}_{seq}.xml"
if not (os.path.exists(path) and os.path.getsize(path)>1500):
if i: time.sleep(8)
for delay in [0,12,24]:
if delay: time.sleep(delay)
r=subprocess.run(["curl","-sS","-G","--http1.1","-A","Mozilla/5.0","--max-time","90","-o",path,
"--data-urlencode","OC=test","--data-urlencode","target=prec",
"--data-urlencode",f"ID={seq}","--data-urlencode","type=XML",
"http://www.law.go.kr/DRF/lawService.do"],capture_output=True,text=True)
if r.returncode==0 and os.path.exists(path) and os.path.getsize(path)>1500: break
if not os.path.exists(path): print(tag,"FAIL"); continue
x=open(path,encoding='utf-8').read()
g=lambda t: (cd(re.search(r'<%s>(.*?)%s>'%(t,t),x,re.S).group(1)) if re.search(r'<%s>'%t,x) else '?')
print(tag, seq, "|", g('사건번호'), "|", g('선고일자'), "|", g('법원명'), "|", g('사건명')[:40], "|판결유형:",g('판결유형'), "|", g('판시사항')[:120].replace('\n',' '))