Pythonのお勉強 Part4
■ このスレッドは過去ログ倉庫に格納されています
0128121
NGNG記法は今風にしてみた。要 Python 2.3。
# -*- coding: EUC-JP -*-
import codecs, re, sys, urllib
# Expected page structure:
# <a href="/business/"><font size="-1" color="#000000"><b>円相場</b> 108円42銭 (18:39)</font></a>
PATTERN = u'<a href="/business/"><font size="-1" color="#000000"><b>円相場</b> '
re_yen = re.compile(ur"(\d+)円(\d+)銭 \((\d\d):(\d\d)\)")
Reader = codecs.getreader("EUC-JP")
urlopener = urllib.FancyURLopener()
urlopener.addheader("Pragma", "no-cache")
file = Reader(urlopener.open("http://www.asahi.com/"))
for line in file:
pos = line.find(PATTERN)
if pos < 0:
continue
match = re_yen.match(line, pos + len(PATTERN))
if not match:
raise RuntimeError, "unexpected page structure"
output = u"%s時%s分現在の円相場は%s円%s銭です。\n" % (
match.group(3), match.group(4),
match.group(1), match.group(2))
sys.stdout.write(output.encode("EUC-JP")) # environment dependent
break
else:
raise RuntimeError, "unexpected end of file"
file.close()
■ このスレッドは過去ログ倉庫に格納されています