DEV Community

Cover image for I Built a Price Tracking Tool Using Python and Web Scraping
Kenn Williams
Kenn Williams

Posted on Edited on

I Built a Price Tracking Tool Using Python and Web Scraping

I often find myself checking the same products across different online stores to see whether their prices have changed. Sometimes a product becomes cheaper after a few days, while other times the price stays exactly the same.

Doing this manually can become frustrating, especially when comparing products across multiple websites.

That made me curious: Could I build a simple tool that automatically checks product prices and keeps track of changes?

I decided to experiment with Python and web scraping. The goal was simple: create a small price tracking tool that collects product information, stores prices, and helps identify when a product becomes cheaper.

In this article, I'll walk through how I approached the project and share the basic code behind it.

1. Understanding the Idea Behind Price Tracking

Before writing any code, I wanted to understand what the tool actually needed to do.

A basic price tracking system should be able to:

  • Collect product names and prices from supported websites.
  • Save the collected information.
  • Check prices again after some time.
  • Compare current prices with previous records.
  • Identify products whose prices have decreased.

Think about someone browsing the top online shopping sites to compare the price of a pair of headphones. Instead of opening several tabs every day, a tracking tool could collect the available prices and organize them in one place.

This is also connected to consumer behavior examples. People often compare prices, wait for discounts, and revisit products before making a purchase. Tracking price changes can make these decisions easier.

2. Setting Up the Python Environment

For this project, I used Python because it offers simple libraries for collecting and processing web data.

We'll need three main libraries:

  • requests – to send HTTP requests.
  • BeautifulSoup – to extract information from HTML.
  • csv – to store product prices.

Install the required libraries:

pip install requests beautifulsoup4

The CSV module is already included in Python, so there's no need to install it separately.

3. Collecting Product Information

The first step is retrieving product information from a webpage.

For this example, I'm using a simple HTML structure to demonstrate how scraping works. In a real project, you should use a website that permits automated access or an official product API.

Here is a basic example:

import requests
from bs4 import BeautifulSoup

url = "https://example.com/product"

headers = {
    "User-Agent": "PriceTracker/1.0"
}

response = requests.get(
    url,
    headers=headers,
    timeout=10
)

response.raise_for_status()

soup = BeautifulSoup(
    response.text,
    "html.parser"
)

product_name = soup.select_one(".product-title")
product_price = soup.select_one(".product-price")

if product_name and product_price:
    print("Product:", product_name.get_text(strip=True))
    print("Price:", product_price.get_text(strip=True))
else:
    print("Product information was not found.")
Enter fullscreen mode Exit fullscreen mode

The CSS selectors .product-title and .product-price are examples. They must be replaced with the actual selectors used by the website you're working with.

This is an important part of web scraping: every website has its own HTML structure, so the extraction logic needs to match the page.

4. Saving Prices in a CSV File

Collecting prices is useful, but saving them allows us to compare changes over time.

I used Python's built-in CSV module to create a simple record of product prices.

import csv
from datetime import datetime

def save_price(product, price, filename="prices.csv"):

with open(
    filename,
    "a",
    newline="",
    encoding="utf-8"
) as file:

    writer = csv.writer(file)

    writer.writerow([
        datetime.now().isoformat(timespec="seconds"),
        product,
        price
    ])
Enter fullscreen mode Exit fullscreen mode

save_price("Wireless Headphones", 89.99)

Every time the function runs, it adds a new record containing the date, product name, and price.

After several checks, the CSV file might look like this:

Date Product Price
Nov 18 Wireless Headphones €99.99
Nov 19 Wireless Headphones €94.99
Nov 20 Wireless Headphones €89.99

Now we have something useful: a basic price history.

5. Detecting Price Changes

The next step is comparing the latest price with the previous recorded price.

def check_price_drop(old_price, new_price):

if new_price < old_price:
    difference = old_price - new_price

    print(
        f"Price dropped by €{difference:.2f}"
    )

elif new_price > old_price:
    print("The price has increased.")

else:
    print("The price has not changed.")
Enter fullscreen mode Exit fullscreen mode

check_price_drop(99.99, 89.99)

Output:

Price dropped by €10.00

This simple comparison is the foundation of a price alert system.

A more advanced version could send an email or notification whenever a product reaches a price that the user has selected.

6. Connecting Price Tracking With Online Shopping

One interesting thing about this project is how it connects programming with everyday shopping decisions.

For example, someone might add a product to their online shopping cart but decide not to purchase it immediately. They may want to compare prices, wait for a promotion, or check whether another retailer offers a better deal.

A price tracking tool can help organize this information and make comparisons easier.

Top saving platforms such as Ozbargain and EMUCoupon also fit into this broader online shopping experience by helping shoppers discover available discounts and promotional offers.

Price monitoring and coupon discovery serve slightly different purposes, but both can help shoppers make more informed purchasing decisions.

7. What I Would Improve Next

The first version of my tool is intentionally simple. However, there are several ways I would improve it:

  • Database integration: Replace CSV storage with SQLite or PostgreSQL.
  • Automated scheduling: Use a scheduler to check prices periodically.
  • Price history charts: Visualize how product prices change over time.
  • Email notifications: Notify users when prices fall below their target.
  • Multiple product support: Track several products from different supported retailers.
  • Dashboard: Build a simple interface using Flask or Streamlit.

I would also add better error handling, respect website terms of service and robots.txt guidance, and use official APIs wherever possible.

Key Takeaways

Building a price tracking tool with Python showed me how a relatively small programming project can solve an everyday problem.

The combination of web scraping, data storage, and price comparison provides a useful starting point for anyone interested in automation and e-commerce development.

You don't need to build a complicated platform immediately. Start with one product, collect its price, save the data, and gradually add more features.
Sometimes the best coding projects begin with a simple question: Can I automate something I keep doing manually?

Top comments (0)