Don't like ads? PRO users don't see any ads ;-)
Guest

shopping_list

By: a guest on Jun 26th, 2012  |  syntax: Python  |  size: 0.95 KB  |  hits: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/env python
  2.  
  3. from BeautifulSoup import BeautifulSoup as BS
  4. from urllib2 import urlopen
  5. from sqlite3 import dbapi2 as sqlite
  6. from time import time, strftime, sleep
  7.  
  8. conn = sqlite.connect("data.db")
  9. cur = conn.cursor()
  10. try:
  11.         cur.execute("CREATE TABLE shopping_list (id INTEGER PRIMARY KEY, url, title, tag_name, tag_class, price)")
  12. except Exception as e:
  13.         print "error: %s -- %s" % (type(e),e)
  14. tick = time()
  15.  
  16.  
  17.  
  18. while True:
  19.         shopping_list = cur.execute("SELECT * FROM shopping_list")
  20.         for item in shopping_list:
  21.                 soup = BS(urlopen(item[1]).read())
  22.                 try:
  23.                         price = soup.find(item[3],{"class":item[4]}).text
  24.                         if price != item[5]:
  25.                                 cur.execute("UPDATE shopping_list SET price = '%s' WHERE id = %s" % (price,item[0]))
  26.                                 print "%s %s now costs: %s" % (strftime("%D %T"), item[2], price)
  27.                                 conn.commit()
  28.                 except Exception as e:
  29.                         print "error: %s -- %s" % (type(e),e)
  30.         while tick % 3600 > 10:
  31.                 sleep(3)
  32.                 tick = time()