Head First Programming
A learner's guide to programming
using the Python language
( O'Reilly Media; )
David Griffiths (著) Paul Barry (著)
開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Python (プログラミング言語)
Head First Programming A learner's guide to programming using the Python language (David Griffiths(著)、Paul Barry(著)、 O'Reilly Media; )のChapter 2(textual data: Every string has its place)、Long Exercise(p.70)を解いてみる。
Long Exercise(p.70)
コード(BBEdit)
sample61.py
#!/usr/bin/env python3
#-*- coding: utf-8 -*-
import urllib.request
import time
price = 99.99
while price > 4.74:
# time.sleep(60 * 15)
# テストのために間隔を短くしたバージョン
time.sleep(2)
# 秒を出力
print(time.gmtime().tm_sec)
page = urllib.request.urlopen('http://beans-r-us.appspot.com/prices.html')
text = page.read().decode('utf8')
where = text.find('>$')
start_of_price = where + 2
end_of_price = start_of_price + 4
price = float(text[start_of_price:end_of_price])
print('price: {0}'.format(price))
print('Buy!')
入出力結果(Terminal)
$ ./sample70.py
41
price: 5.58
44
price: 6.23
46
price: 5.34
49
price: 5.19
51
price: 5.13
54
price: 6.7
56
price: 6.0
59
price: 6.67
1
price: 6.0
4
price: 5.88
C-c C-cTraceback (most recent call last):
File "./sample70.py", line 11, in <module>
time.sleep(2)
KeyboardInterrupt
$
0 コメント:
コメントを投稿