| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
| #! /usr/bin/env python
import urllib
baseurl = "http://tv.yahoo.co.jp/tvguide/vhf/%s/table/%4d%02d%02d%02d.html"
position = "fukuoka"
urls = []
output = ['<html><body><table BORDER="1" CELLPADDING="1" CELLSPACING="1" WIDTH="100%">']
stations = ""
def is_timetable(line):
"""
is_timetable(line) -> 0 or 1
line が番組表の一部かどうかを返す。テレビ局一覧は最初の一回だけ通して、
global変数 stationsに設定しちゃう。
"""
ret = line[1:18] == '<TR ALIGN="left">'
global stations
if ret != 0 and stations == "":
stations = line
else:
if line == stations:
return 0
return ret
# URLの準備
import time
seq = time.time()
(year,month,day,hour,min,sec,wday,jday,dl) = time.localtime(seq)
# ちょっとテキスト追加
output.append(
"<h1>%d年%d月%d日の%sのテレビ番組一覧</h1>" %
(year, month, day, position))
for hour in (4,7,10,13,16,19,21,23):
urls.append(baseurl % (position, year, month, day, hour))
(year,month,day,hour,min,sec,wday,jday,dl) = time.localtime(seq+24*60*60)
hour = 1
urls.append(baseurl % (position, year, month, day, hour))
#データの取得
for url in urls:
try:
data = urllib.urlopen(url)
except IOError:
print 'cannot read [%s]!!' % url
try:
for line in data.readlines():
if is_timetable(line):
output.append(line)
finally:
data.close()
output.append(stations)
output.append('</table></body></html>')
# 出力
for line in output:
print line, |