DEV Community

Quill Datafix
Quill Datafix

Posted on Edited on

Watch Any Product Price With a 40-Line Python Script (Free, No API Key)

I keep a short watchlist: a laptop I want under $700, a SSD under $60, a specific coffee that shouldn't cost more than $9. Checking five pages manually every morning is exactly the kind of boring work a small script does better.

Here's the whole watcher. Standard library only - no pip installs, no API keys, no paid data feeds.

import urllib.request, json, re, time

WATCHLIST = [
    {"url": "https://example.com/product", "max": 700.00, "pattern": r"\$\s?([0-9,.]+)"},
]

def get_price(url, pattern):
    req = urllib.request.Request(url, headers={"User-Agent": "Mozilla/5.0"})
    html = urllib.request.urlopen(req, timeout=20).read().decode("utf-8", "ignore")
    m = re.search(pattern, html)
    return float(m.group(1).replace(",", "")) if m else None

state_file = "prices.json"
try:
    state = json.load(open(state_file))
except FileNotFoundError:
    state = {}

for item in WATCHLIST:
    price = get_price(item["url"], item["pattern"])
    if price is None:
        continue
    old = state.get(item["url"])
    state[item["url"]] = price
    if old is not None and price <= item["max"] and (old is None or old > item["max"]):
        print(f"DROPPED: {item['url']} now {price} (target {item['max']})")
    elif old is not None and price != old:
        print(f"changed: {item['url']} {old} -> {price}")

json.dump(state, open(state_file, "w"))
Enter fullscreen mode Exit fullscreen mode

Run it from cron (0 9 * * * python3 watcher.py) and it prints a line whenever a price crosses your target or just moves. Swap the print for smtplib if you want an email instead.

Three things I learned running this for a while:

  1. The regex is the product. Every site hides its price differently - some in JSON-LD, some in a <meta itemprop>, some in a span with a class that changes every deploy. Grab the page, search for the price pattern, and check it still matches after every run.
  2. One page per site is enough. You don't need a crawler. You need the same page a human would open.
  3. Keep the state file small. URL -> last price. That's it. When the file is 10 lines you will actually maintain it.

Update: the watcher is now installable from PyPI - pip install price-monitor-kit gives you the hardened version as a price-monitor command (config file, CSV history, Telegram/email alerts, JSON-LD detection). Same idea as the script below, plus the flaky-network armor.

I packaged my hardened version (retry on flaky networks, per-site config, JSON-LD detection, a cron template, and a troubleshooting checklist) as Price Monitor Kit - it's one dollar, because I'd rather collect honest first feedback than set a high price: datafixtools.sell.app/product/price-monitor-kit

If you'd rather learn than pay, everything above is enough to build your own - the kit only saves you the debugging evenings.

I maintain the product linked above. Prices on real sites vary; test the regex against your target page before trusting an alert.

Top comments (0)