Practical guide

Building a Python Price Tracker with Crawlee and Apify

Learn to build a reliable price monitoring tool using Crawlee for Python and Apify. This guide covers setup, data extraction, email alerts, and cloud deployment for automated tracking.

A code editor displaying Python script for web scraping with Crawlee and Apify

Monitoring product prices manually is inefficient and prone to error. By leveraging Crawlee for Python alongside the Apify platform, developers can automate the extraction of product details and trigger notifications when prices drop. This approach combines robust web scraping capabilities with cloud-based scheduling and storage, creating a maintainable solution for e-commerce monitoring.

Context and practical value

The source provides a step-by-step tutorial on building a price tracker using Crawlee for Python and Apify. It covers project setup, customizing selectors for product data, implementing email alerts via an Apify actor, and deploying the solution to the cloud with scheduled runs.

AtlasRepo structures the tutorial into a clear, actionable guide for developers, emphasizing the separation of concerns between data extraction, logic, and deployment. It highlights the importance of data type conversion and error handling in production environments.

Key takeaways

  • Use the Apify CLI to scaffold a Python project with pre-built Crawlee templates.
  • Extract structured data by targeting specific CSS selectors and HTML attributes.
  • Convert string prices to floats to enable accurate numerical comparisons.
  • Trigger external actors, such as email services, based on conditional logic.
  • Deploy and schedule crawlers on the Apify cloud for continuous monitoring.

Project Initialization

Begin by installing the Apify CLI via Homebrew or NPM. Use the command 'apify create' to generate a new project, selecting the Python and Crawlee + BeautifulSoup template. This provides a functional boilerplate that scrapes a default page, allowing you to verify your environment immediately by running 'apify run' and checking the storage/datasets directory for JSON output.

Customizing Data Extraction

Modify the main.py file to target your desired URL. Replace the default start URL with the specific product page. Inside the request handler, use BeautifulSoup to locate elements containing the product name and price. For the price, extract the value from a data attribute (e.g., data-price-amount) rather than text content to avoid currency symbols. Convert this value to a float to facilitate mathematical operations.

Implementing Alert Logic

Define a price threshold variable. After extracting the data, compare the current price against this threshold. If the price is lower, use the Apify SDK to start a 'Send Email' actor. Pass the recipient email, subject, and a formatted message containing the product name, new price, and URL. Ensure you are logged into Apify via the CLI so the API token is available for authentication.

Deployment and Scheduling

Push your code to the Apify cloud using 'apify push'. Once deployed, update the start URLs in the actor settings if they were reset. To automate the process, use the scheduling feature in the Apify dashboard to set a recurring interval, such as every hour. This ensures the crawler runs periodically without manual intervention, sending alerts only when conditions are met.

Practical next steps

  1. Install the Apify CLI and create a new Python project using the Crawlee template.
  2. Identify CSS selectors for product name and price on your target website using browser DevTools.
  3. Implement logic to convert extracted price strings to floats and compare them against a threshold.
  4. Configure an email actor integration to send notifications when the price condition is satisfied.
  5. Deploy the actor to Apify and set up a schedule for regular execution.

Limits and verification

  • The solution relies on specific CSS selectors and HTML structures, which may break if the target website updates its layout.
  • Email delivery depends on the third-party 'Send Email' actor and may be subject to spam filters or rate limits.
  • The example uses a hardcoded URL; scaling to multiple products requires dynamic input handling or a list of URLs.

FAQ

Do I need an Apify account to run this locally?

You can run the crawler locally without an account, but sending emails via the Apify actor requires authentication with an API token.

How do I handle price formats with currency symbols?

Extract the price from a data attribute if available, or use string manipulation like replace('$', '') before converting to float.

Can I track multiple products with this script?

Yes, by modifying the start_urls list to include multiple product links and adjusting the data storage to distinguish between items.