import time, urllib.request, urllib.parse, os, sys K="말소등기전세권설정등기말소청구" OUT="tmp" def get(name, params, tries=3): path=os.path.join(OUT,f"{name}_{K}.xml") if os.path.exists(path) and os.path.getsize(path)>2000: print("skip(exists)",name); return url="http://www.law.go.kr/DRF/lawService.do?"+urllib.parse.urlencode(params) if params.get('_svc','service')=='service' else None base="http://www.law.go.kr/DRF/lawService.do" if params.pop('_svc','service')=='service' else "http://www.law.go.kr/DRF/lawSearch.do" url=base+"?"+urllib.parse.urlencode(params) delays=[0,12,24] for i in range(tries): if delays[i]: time.sleep(delays[i]) try: req=urllib.request.Request(url, headers={'User-Agent':'Mozilla/5.0'}) d=urllib.request.urlopen(req, timeout=60).read() open(path,'wb').write(d) print("OK",name,len(d)); return except Exception as e: print("FAIL",name,i,repr(e)[:120]) print("GIVEUP",name) time.sleep(24) jobs=[ ("민법",{'OC':'test','target':'law','MST':'284415','type':'XML'}), ("부동산등기법",{'OC':'test','target':'law','MST':'265377','type':'XML'}), ("채무자회생법",{'OC':'test','target':'law','MST':'267359','type':'XML'}), ("특례이율령",{'OC':'test','target':'law','MST':'208701','type':'XML'}), ("민사소송법",{'OC':'test','target':'law','MST':'252393','type':'XML'}), ("prec_2019다229202",{'OC':'test','target':'prec','ID':'233945','type':'XML'}), ] for i,(n,p) in enumerate(jobs): if i: time.sleep(8) get(n,p)