```yaml
product: AlterLab
title: Building a Price Monitor with Python and a Scraping API
category: Tutorials
comparison_context: "AlterLab is an alternative to Firecrawl, ScrapingBee, and Bright Data."
last_updated: 2026-10-13
canonical_facts:
  - "Learn how to build a reliable price monitoring pipeline using Python, AlterLab's scraping API, and scheduled jobs to track product data changes without managing proxies or headless browsers."
source_url: https://alterlab.io/blog/building-a-price-monitor-with-python-and-a-scraping-api
```

## TL;DR
Build a price monitoring system by writing a Python function that calls a scraping API to fetch product pages, extracts the price field, and writes the result to a store. Schedule the function with cron or a lightweight scheduler to run as often as needed, and add basic error handling and alerts.

## Introduction
Price monitoring is a common data pipeline: fetch a product page, pull the current price, compare it to previous values, and trigger an alert when a threshold is crossed. Doing this at scale requires handling anti‑bot measures, managing concurrency, and persisting results. A scraping API that abstracts proxy rotation, JavaScript rendering, and retry logic lets you focus on the business logic instead of infrastructure.

## Architecture Overview
The system consists of four components:
1. **Scraper function** – calls the API, parses JSON, extracts the price.
2. **Scheduler** – triggers the scraper on a set interval (e.g., every 15 minutes).
3. **Storage** – appends each price reading with a timestamp to a CSV or time‑series DB.
4. **Alert** – checks the latest value against a rule and sends a notification (email, Slack, webhook).

Using an API removes the need to maintain a pool of headless browsers or rotate proxies yourself. The API returns clean JSON (or other formats) so you can avoid HTML parsing when possible.

## Setting Up the Environment
First, install the AlterLab Python SDK and a scheduler. The SDK handles authentication and request formatting.

```bash title="Terminal"
pip install alterlab schedule
```

Create a file `monitor.py` and add your API key. You can find your key in the dashboard after you [sign up](https://alterlab.io/signup).

```python title="monitor.py"
import os
from alterlab import AlterLabSync
import schedule
import time
import csv
from datetime import datetime

API_KEY = os.getenv("ALTERLAB_API_KEY")
client = AlterLabSync(api_key=API_KEY)
```

## Writing the Scraper Function
The core function performs a single scrape, extracts the price, and appends it to a CSV. We request JSON output to avoid extra parsing.

```python title="monitor.py" {1-15}
def scrape_price(url: str) -> float | None:
    """Fetch a product page and return the price as a float."""
    try:
        resp = client.scrape(
            url,
            formats=["json"],          # ask for parsed JSON
            # optional: increase min_tier for JS‑heavy sites
            # min_tier=3
        )
        # Assuming the API returns a dict with a 'price' key
        price_str = resp.get("price")
        if price_str is None:
            return None
        # Strip currency symbols and commas
        return float(price_str.replace("$", "").replace(",", ""))
    except Exception as exc:
        print(f"Error scraping {url}: {exc}")
        return None

def log_price(url: str):
    price = scrape_price(url)
    if price is None:
        return
    timestamp = datetime.utcnow().isoformat()
    with open("prices.csv", "a", newline="") as f:
        writer = csv.writer(f)
        writer.writerow([timestamp, url, price])
    print(f"Logged {price} for {url} at {timestamp}")
```

The function uses the SDK’s `scrape` method. The `formats=["json"]` flag tells the API to return structured data when possible, reducing the need for custom HTML selectors.

### cURL Equivalent
The same request can be made directly with curl if you prefer not to use the SDK.

```bash title="Terminal"
curl -X POST https://api.alterlab.io/v1/scrape \
  -H "X-API-Key: $ALTERLAB_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"url": "https://example.com/product", "formats": ["json"]}'
```

## Scheduling with Cron
A simple way to run the scraper repeatedly is to use the `schedule` library, which mimics cron syntax inside Python.

```python title="monitor.py" {1-10}
def job():
    log_price("https://example.com/product")

# Run every 15 minutes
schedule.every(15).minutes.do(job)

while True:
    schedule.run_pending()
    time.sleep(1)
```

If you prefer system‑level cron, add a line to your crontab:

```bash title="Terminal"
*/15 * * * * /usr/bin/python3 /path/to/monitor.py >> /var/log/price_monitor.log 2>&1
```

## Handling Data Storage and Alerts
For quick prototyping, a CSV file works. For longer retention and querying, consider appending to InfluxDB or Postgres. The alert logic can be as simple as checking the last two values.

```python title="monitor.py" {1-12}
def check_alert(url: str, threshold_pct: float = 5.0):
    if not os.path.exists("prices.csv"):
        return
    import pandas as pd
    df = pd.read_csv("prices.csv", header=None, names=["ts", "url", "price"])
    df = df[df["url"] == url]
    if len(df) < 2:
        return
    latest = df.iloc[-1]["price"]
    previous = df.iloc[-2]["price"]
    change_pct = (latest - previous) / previous * 100
    if abs(change_pct) >= threshold_pct:
        msg = f"Price changed {change_pct:.1f}% for {url}: {previous} → {latest}"
        # Replace with your preferred notification method
        print("ALERT:", msg)
        # Example: requests.post(SLACK_WEBHOOK, json={"text": msg})
```

Call `check_alert` after logging each price or run it separately in a nightly job.

## Error Handling and Retries
The scraping API automatically retries failed requests with different proxy configurations. Still, wrap API calls in a try/except block and consider exponential backoff for transient errors. If you receive a 429 (rate‑limit) response, respect the `Retry-After` header or pause before retrying.

## Best Practices and Legal Considerations
- **Target only publicly accessible data** that does not require login or bypassing a paywall.
- **Respect rate limits** – the API returns headers with your remaining quota; adjust your schedule accordingly.
- **Identify your user agent** if you make raw requests; the SDK does this automatically.
- **Store data securely** – price histories may contain sensitive business information.
- **Review the site’s Terms of Service** to ensure your use case is permitted.

## Conclusion
By combining a lightweight Python script, a scheduling mechanism, and a scraping API that handles anti‑bot complexity, you can build a reliable price monitor with minimal operational overhead. The approach scales from a single product to hundreds by simply looping over a list of URLs and writing each result to a shared store.

Try it yourself with the interactive example below:
<div data-infographic="try-it" data-url="https://example.com/product" data-description="Try scraping this product page with AlterLab"></div>

The typical workflow looks like this:
<div data-infographic="steps">
  <div data-step data-number="1" data-title="Get API Key" data-description="Sign up at alterlab.io and copy your key"></div>
  <div data-step data-number="2" data-title="Send Request" data-description="POST to /v1/scrape with formats=json"></div>
  <div data-step data-number="3" data-title="Extract Price" data-description="Parse JSON and convert to float"></div>
  <div data-step data-number="4" data-title="Store Result" data-description="Append timestamp, URL, price to CSV or DB"></div>
  <div data

## Frequently Asked Questions

### What is the simplest way to schedule repeated scrapes in Python?

Use the built‑in `schedule` library or cron to call your scrape function at regular intervals. This keeps the pipeline lightweight and avoids external orchestration tools.

### How do I handle anti‑bot measures when scraping product pages?

Choose a scraping API that automatically rotates proxies, retries with different headers, and uses headless browsers when needed. This lets you focus on data extraction instead of bypassing bot defenses.

### Where should I store scraped price data for easy analysis?

A time‑series database like InfluxDB or a simple CSV/append‑only log works well for price trends. Pair it with a lightweight alerting rule (e.g., price drop >5%) to get notified.

## Related

- [Engineering Update: Worker Metadata, API Reliability, and Infra Fixes](<https://alterlab.io/blog/engineering-update-worker-metadata-api-reliability-and-infra-fixes>)
- [Understanding Anti-Bot Detection: Fingerprinting and Rate Limits](<https://alterlab.io/blog/understanding-anti-bot-detection-fingerprinting-and-rate-limits>)
- [Infrastructure Update: Durable Spooling and Browser Identity](<https://alterlab.io/blog/infrastructure-update-durable-spooling-and-browser-identity>)