# -*- coding: utf-8 -*- import urllib.request, ssl, re, http.cookiejar ctx=ssl.create_default_context(); ctx.check_hostname=False; ctx.verify_mode=ssl.CERT_NONE cj=http.cookiejar.CookieJar(); op=urllib.request.build_opener(urllib.request.HTTPSHandler(context=ctx),urllib.request.HTTPCookieProcessor(cj)) op.addheaders=[('User-Agent','Mozilla/5.0')] def fixdom(u): return str(u).replace('https://new.igbf.kr','http://www.igbf.kr') def g(u): return op.open(fixdom(u),timeout=20).read().decode('utf-8','replace') def koglblock(h): i=h.lower().find('kogl') if i<0: return None return re.sub(r'\s+',' ', re.sub(r'<[^>]+>',' ', h[max(0,i-400):i+400])) for nm,u in [('우리민요반주(2유형)','http://www.igbf.kr/gugak_web/?sub_num=849'), ('편성표','http://www.igbf.kr/gugak_web/?sub_num=786'), ('국악교육자료','http://www.igbf.kr/gugak_web/?sub_num=770'), ('국악방송소개','http://www.igbf.kr/gugak_web/?sub_num=754')]: h=g(u) has='kogl' in h.lower() # kogl img src koglimg=re.findall(r']+src="([^"]*kogl[^"]*)"', h, re.I) # 조건 텍스트 cond=re.findall(r'출처표시(?:-상업적이용금지)?(?:-변경금지)?', h) print('%s: kogl요소=%s img=%s 조건텍스트=%s'%(nm, has, koglimg[:3], set(cond))) if has: print(' blk:', koglblock(h)[:200])