SHARE
TWEET

Amazon.it Price Analyzer

a guest Nov 25th, 2011 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python2
  2.  
  3.  
  4. import os
  5. import re
  6. import urllib
  7.  
  8. # Aggiungi i codici che trovi in nell'indirizzo della pagina dell'oggetto
  9. # come elementi della variabile %codes
  10. # (es: http://www.amazon.it/Call-Duty-Modern-Warfare-3/dp/B005DEZRA6/
  11. # codice=B005DEZRA6)
  12. codes = [
  13.     'B0002UYA8Q', # Summit [Edizione: Germania]
  14.     'B000268AZE', # Reunion Cumbre [Edizione: Germania]
  15.     'B000BYAD20', # Complete Studio Recordings [Edizione: Francia]
  16.     'B00004TY56', # Bossa Antigua [Edizione: Francia]
  17.     'B0012BMMZC', # Getz Meets Mulligan In Hi-Fi [Edizione: Francia]
  18.     'B0000365G8', # Sweet & Lowdown [Colonna sonora]
  19.     ]
  20.  
  21. # Cambia il nome del file come preferisci, ora come ora salva sulla home un
  22. # file di "nome AmazonPriceHistory.log"
  23. logFilename = os.path.join(os.getenv('HOME'),'AmazonPriceHistory.log')
  24. if not os.path.exists(logFilename):
  25.     with open(logFilename,'w') as fp: fp.write('')
  26.  
  27.  
  28. def Elaborate(code):
  29.     p = re.compile(' *<.*(EUR )([^<]*)</b.*')
  30.     d = re.compile(' *<[^>]*>([^<]*)<span.*>')
  31.     address = 'http://www.amazon.it/gp/product/%s/' % ( code )
  32.     content = urllib.urlopen(address)
  33.     content = content.read().splitlines()
  34.     for line in content:
  35.         if line.count('EUR') and line.count('priceLarge'):
  36.             largePrice = eval(p.match(line).group(2).replace(',','.'))
  37.         if line.count("btAsinTitle"):
  38.             itemName = d.match(line).group(1)
  39.     with open(logFilename) as fp:
  40.         fileCont = fp.read().splitlines()
  41.         for idx in range(len(fileCont)):
  42.             items = fileCont[idx].split('\t')
  43.             if items[0] == itemName:
  44.                 items[1] += ',%s' % largePrice
  45.                 fileCont[idx] = '\t'.join(items)
  46.                 break
  47.             if idx == len(fileCont)-1:
  48.                 fileCont.append('%s\t%s' % (itemName, largePrice))
  49.     with open(logFilename, 'w') as fp:
  50.         fp.write('\n'.join(fileCont)+'\n')
  51.     print '%s done: %s > %.2f' % (code, itemName, largePrice)
  52.  
  53. if __name__ == '__main__':
  54.     for ind in codes:
  55.         Elaborate(ind)
  56.  
  57.  
RAW Paste Data
Pastebin PRO Summer Special!
Get 60% OFF on Pastebin PRO accounts!
Top