Skip to content

Instantly share code, notes, and snippets.

@dp7k
Last active February 16, 2017 18:50
Show Gist options
  • Select an option

  • Save dp7k/9fa0ec1603d759e1ccad0de35fe5f6eb to your computer and use it in GitHub Desktop.

Select an option

Save dp7k/9fa0ec1603d759e1ccad0de35fe5f6eb to your computer and use it in GitHub Desktop.
Simple script to watch Amazon.de prices. Optionally store them in your database and make crazy shit.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import re
import requests
'''
URLs
https://www.amazon.de/gp/aw/d/B003SGEPRW
https://www.amazon.de/dp/B003SGEPRW
https://www.amazon.de/Frigeo-Ahoj-Brause-Brause-Brocken-1-er-Pack/dp/B003SGEPRW/ref=sr_1_1/255-7902316-3333004?ie=UTF8&qid=1487266446&sr=8-1&keywords=brausebrocken
'''
class Amazon_Price_Fetcher:
def __init__(self):
pass
def get_price_from_url(self, url):
productid = self._extract_productid_fomr_url(url)
return self.get_price_from_productid(productid)
def get_price_from_productid(self, productid):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.246'
}
url = 'https://www.amazon.de/gp/aw/d/%s' % (productid)
r = requests.get(url, headers=headers)
html = r.text
m = re.search('\<b\>Preis:\</b\>.*?EUR (\d+,\d+).*?<br />', html)
return m.group(1)
def _extract_productid_fomr_url(self, url):
m = re.search('/([A-Z0-9]+)(/|$)', url)
return m.group(1)
# Demo
if __name__ == '__main__':
apf = Amazon_Price_Fetcher()
print( apf.get_price_from_url('https://www.amazon.de/Frigeo-Ahoj-Brause-Brause-Brocken-1-er-Pack/dp/B003SGEPRW/ref=sr_1_1/255-7902316-3333004?ie=UTF8&qid=1487266446&sr=8-1&keywords=brausebrocken') )
print( apf.get_price_from_url('http://www.amazon.de/dp/B003SGEPRW') )
print( apf.get_price_from_productid('B003SGEPRW') )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment