Stop 11 · Automation & ScriptingProjects · 2/3

Price Tracker Scraper

Part of the Python Roadmap

In short

Waiting for a price to drop and checking the same page every day is tedious. This project builds a small tracker that fetches a price, logs it with a timestamp, and tells you when it falls below your target, all running on a schedule.

A script that checks the price of a few products on practice or permitted pages, stores each reading with a timestamp in a CSV, and alerts you when a price drops below your target. It combines scraping, files and scheduling.

The jaadu peers through binoculars at a shop window with a price tag, scribbling in a small diary, while an alarm clock rings on the pavement beside it.

Think of it like this

The friend who watches the sale

Your cousin in Surat walks past the electronics shop every morning, notes the headphone price in a little diary, and calls you the day it drops below your budget. The tracker is that cousin: fetch, note it down, compare, and alert.

Watch it happen

One run of the tracker

Fetch
Parse
Log CSV
Alert?

1/4requests downloads the product page with a timeout and a clear User-Agent.

The big ideas

What you need to know

Scrape responsibly

Practise on sites made for it, like books.toscrape.com. For real shops, read the terms and robots.txt, and prefer an official API.

Precise selectors

Inspect the page in your browser to find the price element's tag and class, then use soup.select_one.

Append, don't overwrite

Open the CSV in "a" mode so every run adds one row and you build a price history.

Schedule it

Run once a day with cron or Task Scheduler. There's no need to hit a site every minute.

See it in code

Walk through the code

python
1import csv2import re3from datetime import datetime4from pathlib import Path5 6import requests7from bs4 import BeautifulSoup8 9URL = "https://books.toscrape.com/catalogue/a-light-in-the-attic_1000/index.html"10TARGET = 50.011LOG = Path("prices.csv")12HEADERS = {"User-Agent": "price-tracker-practice/1.0"}13 14 15def fetch_price(url: str) -> float:16    response = requests.get(url, headers=HEADERS, timeout=10)17    response.raise_for_status()18    soup = BeautifulSoup(response.text, "html.parser")19    tag = soup.select_one("p.price_color")20    if tag is None:21        raise ValueError("price element not found; has the page changed?")22    return float(re.sub(r"[^0-9.]", "", tag.get_text()))23 24 25def log_price(price: float) -> None:26    is_new = not LOG.exists()27    with LOG.open("a", newline="", encoding="utf-8") as f:28        writer = csv.writer(f)29        if is_new:30            writer.writerow(["checked_at", "price"])31        writer.writerow([datetime.now().isoformat(timespec="seconds"), price])32 33 34if __name__ == "__main__":35    price = fetch_price(URL)36    log_price(price)37    if price <= TARGET:38        print(f"Price drop! Now {price}, at or below your target of {TARGET}.")39    else:40        print(f"Still {price}. Waiting for {TARGET}.")

1/5Settings at the top: the page, your target price, the log file and an honest User-Agent.

pip install requests beautifulsoup4. The practice site lists prices in pounds; the same code works for any rupee price you're allowed to fetch.

Save yourself the pain

Common mistakes

Oops

Scraping a shopping site every few seconds.

Do this instead

Once or twice a day is plenty for prices. Heavy scraping can get you blocked and may break the site's terms.

Oops

Assuming the price element will always be there.

Do this instead

Check for None and fail with a clear message. Sites change their HTML without warning.

Oops

Converting "₹1,299" with float() directly.

Do this instead

Strip the symbol and commas first, for example with re.sub(r"[^0-9.]", "", text).

Your mission

Track three books

~60 min

Quick check

Why does the script open prices.csv with mode "a" instead of "w"?

Go deeper

  1. 1Store product URLs and target prices in a JSON config file.
  2. 2Append each run's price and time to a CSV history file.
  3. 3Schedule the script to run daily and print or email an alert on a price drop.

More projects at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge