How to Build a Price Tracker With Python

Price trackers are handy tools for monitoring product prices on ecommerce websites. As an experienced Python developer, I‘ve built several price trackers over the years. In this comprehensive guide, I‘ll walk through the step-by-step process of building a Python price tracker from scratch.

Overview

A price tracker is a script that regularly checks product prices on ecommerce sites. It extracts pricing data, compares it against previous prices, and can send alerts when prices change significantly.

Here‘s what we‘ll cover in this guide:

  • Benefits of building a price tracker
  • Required Python modules
  • Reading product URLs from a CSV file
  • Scraping product prices
  • Saving price data to a CSV file
  • Sending email alerts on price changes
  • Scheduling the tracker to run automatically

By the end, you‘ll have a fully-functional price tracking script ready for real-world use. The techniques can also be extended to more advanced capabilities like tracking competitor prices, integrating with databases, building browser extensions, and more.

Benefits of Price Tracking

Here are some of the main reasons to build a price tracker:

  • Find deals and price drops – Get notified when an item you want drops in price so you can snag it at the best value.

  • Price monitoring – Keep tabs on competitors‘ pricing strategies. Undercut them or match prices to stay competitive.

  • Price optimization – Analyze historical pricing data to determine optimal prices for maximizing sales and revenue.

  • Alerting – Create alerts for clients or customers when specific products hit target price thresholds.

  • Data collection – Compile pricing data on entire product categories for analysis.

  • Dashboards – Display real-time price data visually for tracking changes and trends.

  • Personal finance – Monitor prices on items you want to buy to get the best deals.

The use cases are extensive. Just about any business can benefit from monitoring product pricing data. Even for personal shopping, price trackers can help you save money.

Required Modules

We‘ll use the following Python modules:

  • Requests – For sending HTTP requests to download web pages.

  • Beautiful Soup – Web scraping library to parse and extract data from HTML and XML.

  • pandas – For reading and writing CSV files and working with data frames.

  • smtplib – To send email notifications.

  • schedule – For scheduling the script to run automatically.

Install them using:

pip install requests beautifulsoup4 pandas smtplib schedule

For parsing prices, we‘ll use the Price Parser module. Install it with:

pip install price-parser

Now let‘s look at the scraper code.

Reading Product URLs from CSV

We‘ll store the product URLs in a CSV file like:

product,url,target_price
Nike Shoes,https://example.com/nike-shoes,100
Xbox Series X,https://example.com/xbox-console,500 
...

The CSV contains the product name, URL, and the price we want to monitor.

We can read this into a Pandas DataFrame with:

import pandas as pd

urls_df = pd.read_csv(‘products.csv‘)

This gives us a DataFrame containing all the product URLs ready for scraping.

Scraping Product Prices

To extract the pricing data, we need to:

  1. Fetch the product page HTML using Requests.
  2. Parse the HTML using Beautiful Soup.
  3. Extract the price text.
  4. Convert to a numeric value using Price Parser.

Let‘s define a function to handle scraping each URL:

from price_parser import Price
import requests 
from bs4 import BeautifulSoup

def scrape_price(url):
    page = requests.get(url)
    soup = BeautifulSoup(page.text, ‘html.parser‘)

    # Find price element on page
    price = soup.find(‘span‘, class_=‘product-price‘).get_text()

    # Remove currency symbols
    price = price.replace(‘$‘, ‘‘)

    # Convert text price into number 
    price = Price.fromstring(price).amount_float

    return price

We make a GET request to the URL to download the HTML, then parse it with Beautiful Soup. Next, we use CSS selectors to extract the price text.

Price Parser handles stripping out the currency symbol and converting the price string into a float number for easier comparison.

Extract All Prices

To retrieve all the prices, we loop through each row in the DataFrame and extract the price:

all_prices = []

for index, row in urls_df.iterrows():
    url = row[‘url‘]
    price = scrape_price(url)
    all_prices.append(price)

Now we have a list containing all the latest prices for further processing.

Compare Prices Against Targets

We want to check if any prices have hit their target amount and send an alert.

Let‘s zip the prices together with their targets:

prices_and_targets = zip(all_prices, urls_df[‘target_price‘])

Then we can iterate through and check each price:

alerts = []

for price, target in prices_and_targets:
    if price <= target:
        alerts.append(f‘Price for {product} has reached target ${target}‘)

Any time the price is lower than the target, we add a message to the alerts list for emailing.

Sending Email Alerts

To send the price drop alerts, we‘ll use Python‘s smtplib:

import smtplib

# Connect to SMTP server
smtp = smtplib.SMTP(‘smtp.gmail.com‘, 587)
smtp.starttls() 

# Login 
smtp.login(‘[email protected]‘, ‘password‘)

# Send notification emails 
for alert in alerts:
   message = f‘Subject: Price Drop Alert\n\n{alert}‘
   smtp.sendmail(‘[email protected]‘, ‘[email protected]‘, message) 

# Close connection  
smtp.quit()

This allows sending emails via Gmail to notify when a target price is reached. The same approach works for any email provider.

Saving Results to CSV

Let‘s also save the scraped prices to a CSV file for tracking price history:

import pandas as pd

# Create DataFrame of prices
prices_df = pd.DataFrame(list(zip(urls_df[‘product‘], all_prices)), columns=[‘product‘, ‘current_price‘]) 

# Append to existing CSV
prices_df.to_csv(‘prices.csv‘, mode=‘a‘, header=False) 

Using mode=‘a‘ appends the new prices to the CSV, keeping a historical record.

Scheduling the Scraper

To continuously monitor prices, we need to schedule the script to run on a repeating basis.

The Python schedule module makes this simple:

import schedule

schedule.every(1).hour.do(scraper)

while True:
    schedule.run_pending()

This executes the main scraping function every hour.

You can schedule it for any interval required – daily, weekly, etc. For more complex scheduling, use cron jobs.

This keeps the price tracker running 24/7 to catch price changes in real-time.

Conclusion

In this guide, we built a complete Python price tracking solution from scratch using web scraping.

The script extracts pricing data from product URLs, compares it to target prices, saves results to CSV, and sends email alerts – providing a full-featured monitoring system.

Some ways to extend the price tracker further:

  • Track competitor prices by adding more URLs.

  • Save data to a database rather than CSV for more robust storage.

  • Add a web interface to display pricing graphs and dashboard.

  • Expand the email alerts with customized messaging.

  • Containerize it with Docker for easy deployment.

The code can serve as a template to build more advanced price tracking and monitoring applications.

Let me know if you have any other questions! I have 5 years of experience building Python scrapers and can provide tips on how to robustly scale this for large volumes of URLs.

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.