Build a Competitor Price Tracker with Python
Build a Competitor Price Tracker with Python
Imagine waking up to an email notification that your biggest competitor just dropped their price by 15%—before they even hit the morning rush. You could adjust your strategy instantly, capture the market, and turn a potential loss into a win. That’s the power of a real-time competitor price tracker, and you can build one yourself in Python before lunch.
Stop manually checking competitor websites every day. Let your code do the heavy lifting while you focus on strategy. Here’s how to build a practical, working price tracker that saves hours and gives you a competitive edge.
Why Build Your Own Tracker?
Most businesses rely on expensive SaaS tools for price monitoring, but those often come with hidden costs, limited customization, and rigid alert systems. A custom Python script gives you full control:
- Zero licensing fees – Just use open-source libraries.
- Custom alert logic – Get notified only when prices drop below your threshold.
- Flexible data storage – Save to CSV, databases, or Google Sheets.
- Scalable – Track one product or hundreds with the same code.
Plus, building it yourself teaches you web scraping, automation, and data handling—skills that pay dividends far beyond price tracking.
The Core Architecture
Your price tracker needs three main components:
- Fetcher: Sends HTTP requests to competitor product pages.
- Parser: Extracts price data from HTML using selectors.
- Comparator & Alert: Checks if prices changed and sends notifications.
Let’s build a working version using requests and BeautifulSoup4—the most beginner-friendly stack for scraping static sites.
Step 1: Install Dependencies
First, set up your environment. You’ll need Python 3.7+ and these libraries:
pip install requests beautifulsoup4 pandas
Create a file called price_tracker.py and start importing:
import requests
from bs4 import BeautifulSoup
import pandas as pd
import time
Step 2: Define Your Competitor URLs
List the product pages you want to monitor. For this example, we’ll track two electronics retailers:
COMPETITOR_URLS = [
"https://example-store1.com/product/huawei-p30-pro",
"https://example-store2.com/item/huawei-p30-pro"
]
Note: Replace these with real URLs. Always check a site’s Terms of Service before scraping.
Step 3: Extract Price with BeautifulSoup
Every site structures prices differently. Use your browser’s Developer Tools (right-click → Inspect) to find the HTML tag holding the price. Common patterns include:
<span class="price">$299</span><div data-testid="product-price">149.99</div>
Here’s a reusable function to scrape a price:
def get_price(url):
headers = {"User-Agent": "Mozilla/5.0"} # Mimic a real browser
response = requests.get(url, headers=headers, timeout=10)
if response.status_code != 200:
return None
soup = BeautifulSoup(response.text, "html.parser")
# Adjust this selector based on the actual site
price_element = soup.find("span", class_="price")
if not price_element:
return None
# Clean the price: remove "$", commas, and convert to float
price_str = price_element.text.replace("$", "").replace(",", "").strip()
return float(price_str)
Test it immediately:
for url in COMPETITOR_URLS:
price = get_price(url)
if price:
print(f"{url}: ${price}")
else:
print(f"Failed to scrape: {url}")
Step 4: Compare Against Your Price
Now, let’s add your own product price and compare:
MY_PRICE = 285.00
def compare_prices(my_price, competitors):
results = []
for url, price in competitors:
if price is None:
status = "Error"
elif price < my_price:
status = "⚠️ Competitor is LOWER"
elif price > my_price:
status = "✅ You are LOWER"
else:
status = "🟰 Prices equal"
results.append({"URL": url, "Price": price, "Status": status})
return pd.DataFrame(results)
# Run the tracker
competitor_data = []
for url in COMPETITOR_URLS:
competitor_data.append((url, get_price(url)))
df = compare_prices(MY_PRICE, competitor_data)
print(df)
This gives you a clean table showing who’s cheaper and where you need to adjust.
Step 5: Automate & Alert
To make this actionable, run it daily and send alerts when prices drop. Add email notifications using Python’s built-in smtplib:
import smtplib
from email.message import EmailMessage
def send_alert(low_price_url, low_price, my_price):
msg = EmailMessage()
msg["From"] = "your_email@example.com"
msg["To"] = "your_email@example.com"
msg["Subject"] = f"⚠️ Price Alert: Competitor dropped to ${low_price}"
msg.set_content(f"Competitor at {low_price_url} is now ${low_price}.\nYour price: ${my_price}")
# Use SMTP server (e.g., Gmail, SendGrid)
with smtplib.SMTP("smtp.example.com", 587) as server:
server.starttls()
server.login("your_email@example.com", "your_password")
server.send_message(msg)
Trigger the alert in your main loop:
for url, price in competitor_data:
if price and price < MY_PRICE:
print(f("ALERT: {url} is cheaper!"))
send_alert(url, price, MY_PRICE)
Step 6: Run It Automatically
Use the schedule library or a cron job to run daily:
# Install schedule
pip install schedule
# In your script
import schedule
schedule.every().day.at("09:00").do(run_tracker) # Run at 9 AM daily
Or on Linux/macOS, add to crontab:
0 9 * * * /usr/bin/python3 /path/to/price_tracker.py
Handling Real-World Challenges
Not all sites are static. Here’s how to handle common issues:
| Challenge | Solution |
|---|---|
| Dynamic content (JavaScript) | Use Selenium or Playwright to load pages fully |
| Rate limiting / blocking | Add time.sleep(2) between requests; rotate User-Agent headers |
| Anti-bot detection | Use proxies (e.g., scrapfly.io, rayobyte.com) or specialized APIs |
| Complex selectors | Use CSS selectors or XPath for more precise matching |
For sites like Amazon or eBay, consider using their official APIs instead of scraping to avoid legal issues.
What You Can Do TODAY
- Pick one competitor URL and extract its price selector using browser inspector.
- Run the code above with your URL and test the output.
- Set your own price and see the comparison table.
- Add an email alert for prices below your threshold.
- Schedule it to run daily at 9 AM.
Within an hour, you’ll have a live tracker that works without subscription fees.
Level Up Your Tracker
Once the basics work, add these features:
- Google Sheets integration: Auto-update a sheet with historical prices [1].
-
Price trend charts: Use
matplotlibto visualize drops over time [8]. - Multi-product tracking: Loop through a list of URLs and store in a database [3].
- Dashboard: Build a Flask app to view trends in real time [8].
Final Thoughts
You don’t need expensive tools to stay competitive. With Python, requests, and BeautifulSoup, you can build a powerful price tracker that gives you instant insights and saves hours of manual work.
The best part? You can start today with code you write yourself.
Ready to try it?
👉 Clone the code, plug in your competitor URL, and run it.
👉 Share your results in the comments—what’s the first price drop you caught?
👉 Follow me for more Python automation tutorials that turn boring tasks into smart solutions.
Let’s make data work for you, not the other way around. 🚀
If you found this helpful, consider buying me a coffee ☕ — it keeps these articles coming!
Also check out my AI tools collection: AI 次元世界 — free AI tools for developers.
Top comments (0)