Prompt
How do I automate website monitoring for pricing or inventory changes without constant breakage?
Latest observation
To automate monitoring for pricing or inventory changes without your scraper breaking all the time, design it like a resilient change-detection system, not a one-off script.
1) Prefer official sources first
Before scraping pages, check for:
- Public APIs
- RSS/Atom feeds
- Sitemap updates
- JSON endpoints used by the site
- Email/webhook notifications
These are usually far more stable than HTML scraping.
2) Monitor the smallest stable surface
If you must scrape:
- Target a page section, not the whole page
- Look for stable identifiers like:
data-*attributes- product IDs
- SKU fields
- JSON embedded in the page
- Avoid relying on brittle CSS classes generated by front-end frameworks
3) Use a two-layer extraction strategy
Build:
- Primary extractor: exact selectors/fields
- Fallback extractor: alternative selectors, regex, or embedded JSON parsing
That way, minor page changes don’t immediately break monitoring.
4) Track the data, not just the page
Store the last known:
- price
- stock status
- timestamp
- source URL
- page checksum or DOM snippet hash
Then compare new snapshots against the previous state so you only alert on meaningful changes.
5) Normalize the content before comparing
Prices and inventory text often vary slightly:
- currency symbols
- whitespace
- “In stock” vs “Available”
- “Only 3 left” vs exact stock number
Normalize values into structured fields before diffing.
6) Add robust failure handling
Your monitoring job should:
- retry on transient failures
- back off on 429/503 responses
- rotate user agents carefully if needed
- detect captchas / bot blocks
- mark results as “unknown” instead of falsely reporting “out of stock”
7) Validate with sanity checks
Before alerting:
- price shouldn’t jump 10x unless verified
- inventory shouldn’t flip wildly within seconds unless the source is known to do so
- if one parser field fails but others succeed, don’t discard the whole record
8) Use headless browsing only when necessary
Tools like Playwright or Selenium help with dynamic sites, but they’re heavier and can still break. Use them only if:
- the data is rendered client-side
- there’s no accessible API or HTML endpoint
If possible, inspect network requests and pull the underlying JSON directly instead of parsing the rendered page.
9) Make selector maintenance easy
Structure your code so selectors are configuration, not hardcoded:
- per-site config file
- versioned selectors
- quick switch between strategies
- logging of failures with page snapshots
This makes updates fast when the site changes.
10) Add observability
Log:
- HTTP status codes
- parse success/failure
- detected field values
- page hash changes
- screenshot/HTML snapshot on failure
This helps you distinguish:
- actual site change
- temporary outage
- anti-bot blocking
- parser bug
11) Alert only on confirmed changes
Use a confirmation rule like:
- check twice before alerting
- require the same change in two consecutive runs
- compare against multiple data sources if available
This reduces false positives from transient glitches.
12) Respect site policies
Check:
- robots.txt
- terms of service
- rate limits
Use conservative polling intervals and avoid aggressive scraping.
A practical architecture
A reliable setup often looks like this:
- Scheduler runs every N minutes
- Fetch page/API response
- Parse using primary extractor
- If parse fails, try fallback extractor
- Normalize fields
- Compare with previous record
- If changed, confirm on next run
- Alert via email/Slack/webhook
- Save raw response + parsed output for debugging
Best tools depending on the site
- Static HTML: Requests + BeautifulSoup/lxml
- Dynamic JS site: Playwright
- API-backed site: Requests to the underlying JSON endpoint
- High-scale monitoring: Scrapy, Playwright, or a managed monitoring service
A good rule of thumb
If your monitoring breaks often, the fix is usually to:
- move closer to the source data
- reduce dependence on HTML structure
- add fallback parsing
- store and compare structured snapshots
- improve logging and retry logic
If you want, I can give you:
- a Python example for resilient price monitoring, or
- a Playwright-based setup for dynamic inventory pages.