DEV Community

Cover image for Building a Price Monitoring System with Python and APIs
AlterLab
AlterLab

Posted on • Originally published at alterlab.io

Building a Price Monitoring System with Python and APIs

TL;DR

To build a price monitoring system, you must automate periodic requests to e-commerce URLs, extract price values using structured selectors or LLMs, and store the results in a database to detect delta changes. Using a dedicated Python scraping API simplifies this by handling proxy rotation and JavaScript rendering automatically.

The Architecture of Price Monitoring

Price monitoring requires a pipeline that moves from discovery to storage. You need to track a specific set of URLs, fetch the HTML content, parse the specific price element, and then compare that value to the last known price in your database.

A production-ready system typically consists of four layers:

  1. Scheduler: A cron job or task queue (like Celery) that triggers the scraping job.
  2. Fetcher: The engine that makes the HTTP request and bypasses bot detection.
  3. Parser: The logic that turns raw HTML or JSON into a float value (e.g., 19.99).
  4. Storage & Alerting: A database (PostgreSQL or MongoDB) to track history and a notification service (Slack, Email) for price drops.

Handling Dynamic Content and Bot Detection

Modern e-commerce sites are rarely static HTML. They rely heavily on JavaScript to render prices after the initial page load. If you use a simple requests.get() call, you will often receive a loading screen or a "Please enable JavaScript" message instead of the actual price.

Furthermore, high-frequency scraping triggers security measures. To maintain a stable pipeline, your system needs sophisticated anti-bot handling to manage rotating residential proxies and headless browser sessions. Without this, your IP addresses will be flagged and blocked within minutes.

Implementation: Python vs. cURL

You can interact with a scraping engine using several methods. For rapid prototyping, curl is efficient. For production pipelines, a dedicated Python SDK is preferred for better error handling and type safety.

```bash title="Terminal"
curl -X POST https://api.alterlab.io/v1/scrape \
-H "X-API-Key: YOUR_API_KEY" \
-d '{"url": "https://example.com/product", "render": true}'






```python title="scraper.py" {1-4}

client = alterlab.Client("YOUR_API_KEY")
# The 'ender=True' parameter ensures JavaScript is executed
response = client.scrape("https://example.com/product", render=True) 
print(f"Price Data: {response.json()}")
Enter fullscreen mode Exit fullscreen mode

Data Extraction: Selectors vs. AI

Once you have the HTML, you have two main paths for extraction:

  1. CSS/XPath Selectors: Fast and cheap, but brittle. If the website changes its class name from .price-tag to .product-price, your scraper breaks.
  2. Cortex AI (LLM Extraction): High reliability. You pass the HTML or Markdown to an LLM and ask for "the price in USD." This is resilient to UI changes.
Method Speed Maintenance Cost
CSS Selectors Very Fast High (Breaks often) Minimal
AI Extraction Moderate Low (Resilient) Variable

Building the Monitoring Logic

Here is a complete logic flow for a single product monitor. This script fetches the data and compares it to a local variable representing your database state.

```python title="monitor.py" {1-8}

def check_price_drop(url, last_price):
client = alterlab.Client("YOUR_API_KEY")
# We use the API to handle complex site rendering
data = client.scrape(url, render=True).json()

# Using a simple key for this example
current_price = data.get("price") 

if current_price < last_price:
    return True, current_price
return False, current_price
Enter fullscreen mode Exit fullscreen mode

Example usage

target_url = "https://example.com/item-123"
previous_price = 50.00
dropped, new_price = check_price_drop(target_url, previous_price)

if dropped:
print(f"Alert! Price dropped to {new_price}")




## Scaling the System
When moving from one URL to one million, your primary bottlenecks will be:
- **Concurrency**: How many requests can you fire simultaneously without hitting rate limits?
- **Data Integrity**: Handling malformed HTML or unexpected "Out of Stock" messages.
- **Cost Management**: Managing your [pricing](https://alterlab.io/pricing) to ensure the cost of scraping doesn't exceed the value of the data.

For large-scale operations, we recommend using webhooks. Instead of polling your API for results, configure the engine to push the data to your endpoint as soon as the scrape completes. This reduces the overhead on your server and allows for real-time monitoring.

## Takeaway
Building a price monitor is a matter of automating the fetch-parse-compare loop. To make it production-ready, move away from basic HTTP clients and use an engine that handles JavaScript rendering and proxy rotation. This ensures your data pipeline remains stable even when target websites update their front-end code or security layers.

## FAQ
Q: How do you build a price monitoring system?
A: A price monitoring system is built by scheduling automated web scraping requests to e-commerce sites, extracting price values via selectors or AI, and comparing the new values against a historical database to detect changes.
Q: What is the best language for web scraping?
A: Python is widely considered the best language for web scraping due to its robust ecosystem of libraries like BeautifulSoup, Playwright, and specialized SDKs that simplify API integrations.
Q: How do you handle bot detection when scraping prices?
A: To handle bot detection, use a scraping API that provides rotating proxies, headless browser rendering, and automatic anti-bot handling to mimic human behavior and ensure high success rates.
Enter fullscreen mode Exit fullscreen mode

Top comments (0)