최초작성 |
2005/12/30 23:37:57 |
야후 방송 스케쥴 긁기
급히 쓸일이 있어서 급조했다. 마지막 부분 DB 쿼리 부분은 가라이고, 또다른 python 정규식 활용의 예제가 되겠다.
채널정보 프로그램정보 프로그램정보 ... 채널정보 프로그램정보 프로그램정보 ...
이렇게 된것으로 파악하고 채널정보 부분에서 채널이름을 따고 프로그램정보에서 방송시간과 프로그램이름을 따오도록 했는데 짜다보니 프로그램정보에 채널이름이 들어있어서 이렇게 짜면 더 간단했을거다
프로그램정보 프로그램정보 ... 프로그램정보 프로그램정보 ...
소스
1 # -*- coding: euc-kr -*- 2 import urllib 3 import re 4 5 6 def httpcall(url): 7 return urllib.urlopen(url).read() 8 9 10 11 def cut(text, head, tail, pos = 0): 12 p = text.find(head, pos) 13 if p < 0: 14 raise StandardError("can't find '" + head + "'") 15 p2 = text.find(tail, p) 16 if p2 < 0: 17 raise StandardError("can't find '" + tail + "'") 18 return text[p:p2], p2 19 20 21 def cut_frontpage_part(text): 22 return cut(text, "<!-- frontpage_grid -->", "<!-- /frontpage_grid -->")[0] 23 24 def parse_all_chan_div(text): 25 pos = 0 26 chans = [] 27 while True: 28 try: 29 x, pos = cut(text, """<div class="chan">""", "\n</div>", pos) 30 chans.append(x) 31 except StandardError: 32 return chans 33 34 35 def parse_channel_name(channel): 36 return re.compile("""<h2><a href=".*">(?P<channel_name>\S+)</a></h2>""", re.DOTALL).search(channel,0).group("channel_name") 37 38 def parse_all_programs(channel): 39 results = [] 40 41 pattern = re.compile("""<li class=".*"><div class=".*"><b>(?P<air_time>.*)</b>.*<a href=".*">(?P<program_name>.*)</a>.*</div></li>\n""") 42 pos = 0 43 while True: 44 matched = pattern.search(channel, pos) 45 if matched is None: 46 return results; 47 else: 48 air_time = matched.group("air_time") 49 program_name = matched.group("program_name") 50 program_name = program_name.replace("&", "&"); 51 program_name = program_name.replace(">", ">"); 52 program_name = program_name.replace("<", "<"); 53 54 results.append((air_time, program_name)) 55 pos = matched.end() 56 57 58 59 60 def parse_time(air_time): 61 s, e = air_time.split("-") 62 s = s.strip() 63 e = e.strip() 64 if e[-1] == ':': e = e[:-1] 65 return s, e 66 67 68 def calc_am_pm(tm): 69 if tm[-2:] == "pm" or tm[-2:] == "PM": 70 tm = tm[:-2] 71 h, m = tm.split(":") 72 h, m = int(h), int(m) 73 h += 12 74 return "%02d:%02d" % (h, m) 75 else: 76 tm = tm[:-2] 77 h, m = tm.split(":") 78 h, m = int(h), int(m) 79 return "%02d:%02d" % (h, m) 80 81 82 83 84 if __name__ == "__main__": 85 yahoo_tv_contents = httpcall("http://tv.yahoo.com/") 86 epg_part = cut_frontpage_part(yahoo_tv_contents) 87 channels = parse_all_chan_div(epg_part) 88 for channel in channels: 89 channel_name = parse_channel_name(channel) 90 #print "채널이름: ", channel_name 91 92 programs = parse_all_programs(channel) 93 for air_time, program_name in programs: 94 print "INSERT INTO epg(channel_name, date, start_time, end_time, program_name) VALUES('%s', curdate(), '%s', '%s', '%s')" % ( 95 channel_name, 96 calc_am_pm(parse_time(air_time)[0]), 97 calc_am_pm(parse_time(air_time)[1]), 98 program_name)
하나더
비슷한 용도로 하나더 만들어서 적어둔다
1 # -*- coding: euc-kr -*- 2 3 import urllib 4 import re 5 import sys 6 7 def httpcall(url): 8 return urllib.urlopen(url).read() 9 10 def parse(contents): 11 results = [] 12 pattern = re.compile("""<strong>(?P<time>.*)</strong>.*<a href=".*">(?P<title>.*)</a>\n""") 13 pos = 0 14 while True: 15 matched = pattern.search(contents, pos) 16 if matched is None: 17 return results 18 else: 19 results.append((re.compile("""<div class=".*">\n<h2>(?P<channel>.*)</h2>\n<form method=get action=".*">""").search(contents).group("channel"), 20 matched.group("time").replace(" ",""), 21 matched.group("title").replace("&", "&").replace(">", ">").replace("<", "<"))) 22 pos = matched.end() 23 24 if __name__ == "__main__": 25 if len(sys.argv) != 2: 26 print "Usage: foo.py [URL]" 27 else: 28 print parse(httpcall(sys.argv[1]))
