getting the price of a product on digitec.ch via python3

i wanted to monitor the price on a certain product on the digitec.ch onlineshop. so i created this little python method to get the price. just get the websites html, and regex the price out of it. sure its possible to use lxml or other xml/html parsers for this, but its just much easier with regex. (at least for me)

this script was working well a long time now, but be warned, if digitec changes something on their website structure, this script may break.

it requires the requests package,
install it via pip3 install requests

import requests,re
# purpose:
#   getting the price of a product on digitec.ch
# requirements:
#   pip3 install requests

url = 'https://www.digitec.ch/de/s1/product/noctua-nf-a9-flx-92mm-1x-pc-luefter-5680302'
url2 = 'https://www.digitec.ch/de/s1/product/value-sata-3-kabel-50cm-datenkabel-intern-pc-286767'
url3 = 'https://www.digitec.ch/de/s1/product/sennheiser-momentum-2-wireless-over-ear-schwarz-kopfhoerer-4236200'


def getDigitecPrice(url):
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36'}

    try:
        r = requests.get(url, headers=headers)
        html = r.text
    except requests.exceptions.RequestException as e:
        print('getDigitecPrice:: failed to get the url: %s'%e)
        return False

    # regex
    try:
        regex = r"<\/span> ([\.\d]*)"
        match = re.search(regex, html)

        if match:
            price = match.group(1)
            # if a price has no rappen in it like 329 (it will appear as 329.-) we will catch 329. with our regex
            # so now we just remove the last point to get just 329
            if price[-1:]!='.':
                return float(price)
            else:
                return float(price[:-1])
        else:
            print("getDigitecPrice:: regex not found in string")
            return False
    except Exception as e:
        print('getDigitecPrice:: failed to regex: %s'%e)
        return False

example usage

url = 'https://www.digitec.ch/de/s1/product/noctua-nf-a9-flx-92mm-1x-pc-luefter-5680302'
price = getDigitecPrice(url)
if not price:
    print("failed to get price, something went wrong!")
else:
    print("price of the item is: CHF %4.2f"%price)

example output

price of the item is: CHF 29.90