The Palos Publishing Company

Follow Us On The X Platform @PalosPublishing
Categories We Write About

Build an Amazon price tracker

Creating an Amazon price tracker involves building a tool that monitors the price of a product on Amazon and notifies you when the price drops or changes. Below is a detailed guide and example code to build a basic Amazon price tracker using Python. This includes scraping the product page, extracting the price, and sending alerts.


Key Components:

  1. Web ScrapingExtract product price from Amazon product page.

  2. Price MonitoringPeriodically check for price changes.

  3. NotificationAlert user when price meets a condition.


Important Notes:

  • Amazon’s website uses dynamic content and anti-bot measures, so scraping needs to be done carefully.

  • Use headers to mimic a browser.

  • Respect Amazon’s Terms of Service; do not overload their servers.

  • For production, use APIs or services like Keepa or CamelCamelCamel for more reliable data.


Step 1: Setup Environment

Install necessary libraries:

bash
pip install requests beautifulsoup4 lxml

For email notifications (optional):

bash
pip install yagmail

Step 2: Python Script for Price Tracking

python
import requests from bs4 import BeautifulSoup import time import yagmail # For email notifications (optional) # Replace with your Amazon product URL PRODUCT_URL = 'https://www.amazon.com/dp/B08N5WRWNW/' # Example product # Your target price TARGET_PRICE = 150.00 # Email credentials (optional) EMAIL_USER = 'your_email@gmail.com' EMAIL_PASS = 'your_email_password' RECEIVER_EMAIL = 'receiver_email@gmail.com' headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)" " Chrome/90.0.4430.85 Safari/537.36", "Accept-Language": "en-US,en;q=0.9" } def get_amazon_price(url): response = requests.get(url, headers=headers) soup = BeautifulSoup(response.content, 'lxml') # Try different possible price element IDs price = None price_ids = ['priceblock_ourprice', 'priceblock_dealprice', 'priceblock_saleprice'] for pid in price_ids: price_str = soup.find(id=pid) if price_str: price = price_str.get_text().strip() break if price is None: # Sometimes price is inside a span with class 'a-offscreen' price_span = soup.find('span', {'class': 'a-offscreen'}) if price_span: price = price_span.get_text().strip() if price: # Remove currency symbol and commas, convert to float price = price.replace('$', '').replace(',', '').strip() try: return float(price) except ValueError: return None else: return None def send_email(subject, body): try: yag = yagmail.SMTP(EMAIL_USER, EMAIL_PASS) yag.send(RECEIVER_EMAIL, subject, body) print("Email sent successfully!") except Exception as e: print(f"Error sending email: {e}") def track_price(): price = get_amazon_price(PRODUCT_URL) if price: print(f"Current price: ${price}") if price <= TARGET_PRICE: subject = "Amazon Price Alert!" body = f"The price for your product has dropped to ${price}.nCheck it out here: {PRODUCT_URL}" send_email(subject, body) else: print("Price is still higher than target.") else: print("Failed to retrieve price.") if __name__ == "__main__": # Run tracker every 12 hours (43200 seconds) while True: track_price() time.sleep(43200)

Explanation:

  • get_amazon_price scrapes the product page for the current price.

  • send_email uses yagmail to send an alert when the price is below or equal to the target price.

  • The script runs indefinitely every 12 hours checking the price.


Optional Improvements:

  • Use a database or file to store price history.

  • Integrate SMS or push notifications.

  • Add CLI or web interface.

  • Use proxies or Amazon API to reduce risk of blocking.


This script provides a foundation for an Amazon price tracker. You can customize and expand it according to your needs.

Share this Page your favorite way: Click any app below to share.

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Categories We Write About