
shopping_list
By: a guest on
Jun 26th, 2012 | syntax:
Python | size: 0.95 KB | hits: 19 | expires: Never
#!/usr/bin/env python
from BeautifulSoup import BeautifulSoup as BS
from urllib2 import urlopen
from sqlite3 import dbapi2 as sqlite
from time import time, strftime, sleep
conn = sqlite.connect("data.db")
cur = conn.cursor()
try:
cur.execute("CREATE TABLE shopping_list (id INTEGER PRIMARY KEY, url, title, tag_name, tag_class, price)")
except Exception as e:
print "error: %s -- %s" % (type(e),e)
tick = time()
while True:
shopping_list = cur.execute("SELECT * FROM shopping_list")
for item in shopping_list:
soup = BS(urlopen(item[1]).read())
try:
price = soup.find(item[3],{"class":item[4]}).text
if price != item[5]:
cur.execute("UPDATE shopping_list SET price = '%s' WHERE id = %s" % (price,item[0]))
print "%s %s now costs: %s" % (strftime("%D %T"), item[2], price)
conn.commit()
except Exception as e:
print "error: %s -- %s" % (type(e),e)
while tick % 3600 > 10:
sleep(3)
tick = time()