mkdir amazon-price-tracker
cd amazon-price-tracker
python3 -m venv .venv
# macOS / Linux
source .venv/bin/activate
# Windows PowerShell
.venv\Scripts\Activate.ps1
pip install requests beautifulsoup4 pandas schedule matplotlib
import re
from decimal import Decimal, InvalidOperation
import requests
from bs4 import BeautifulSoup
HEADERS = {
"User-Agent": (
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) "
"AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/155.0.0.0 Safari/537.36"
),
"Accept-Language": "en-US,en;q=0.9",
"Accept": (
"text/html,application/xhtml+xml,application/xml;q=0.9,"
"image/avif,image/webp,*/*;q=0.8"
),
}
PRICE_SELECTORS = [
"#corePriceDisplay_desktop_feature_div span.a-offscreen",
"#corePrice_feature_div span.a-offscreen",
"span.priceToPay span.a-offscreen",
".apexPriceToPay span.a-offscreen",
]
def parse_price(text: str) -> Decimal:
cleaned = re.sub(r"[^\d,.\-]", "", text.strip())
if not cleaned:
raise ValueError(f"No numeric price found in {text!r}")
# Handles formats such as:
# 1,299.99
# 1.299,99
# 29,99
# 1,299
if "," in cleaned and "." in cleaned:
decimal_separator = (
"," if cleaned.rfind(",") > cleaned.rfind(".") else "."
)
thousands_separator = "." if decimal_separator == "," else ","
cleaned = cleaned.replace(thousands_separator, "")
cleaned = cleaned.replace(decimal_separator, ".")
elif "," in cleaned:
parts = cleaned.split(",")
if len(parts[-1]) in (1, 2):
cleaned = "".join(parts[:-1]) + "." + parts[-1]
else:
cleaned = "".join(parts)
elif "." in cleaned:
parts = cleaned.split(".")
if len(parts) > 2 or len(parts[-1]) == 3:
cleaned = "".join(parts)
try:
return Decimal(cleaned)
except InvalidOperation as exc:
raise ValueError(f"Could not parse price: {text!r}") from exc
def scrape_product(url: str) -> dict:
response = requests.get(
url,
headers=HEADERS,
timeout=20,
)
response.raise_for_status()
soup = BeautifulSoup(response.text, "html.parser")
title_element = soup.select_one("#productTitle")
if title_element is None:
raise RuntimeError("Product title was not found.")
price_element = None
matched_selector = None
for selector in PRICE_SELECTORS:
candidate = soup.select_one(selector)
if candidate and candidate.get_text(strip=True):
price_element = candidate
matched_selector = selector
break
if price_element is None:
raise RuntimeError(
"No main product price was found. "
"The page layout may have changed."
)
return {
"title": title_element.get_text(" ", strip=True),
"price": parse_price(price_element.get_text(strip=True)),
"selector": matched_selector,
"url": url,
}
if __name__ == "__main__":
product = scrape_product(
"https://www.amazon.com/dp/YOUR_ASIN_HERE"
)
print(product)
import re
from decimal import Decimal, InvalidOperation
from bs4 import BeautifulSoup
from requests import Session
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
HEADERS = {
"User-Agent": (
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) "
"AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/155.0.0.0 Safari/537.36"
),
"Accept-Language": "en-US,en;q=0.9",
"Accept": (
"text/html,application/xhtml+xml,application/xml;q=0.9,"
"image/avif,image/webp,*/*;q=0.8"
),
}
PRICE_SELECTORS = [
"#corePriceDisplay_desktop_feature_div span.a-offscreen",
"#corePrice_feature_div span.a-offscreen",
"span.priceToPay span.a-offscreen",
".apexPriceToPay span.a-offscreen",
]
def build_session() -> Session:
retry_policy = Retry(
total=4,
connect=3,
read=3,
status=4,
backoff_factor=2,
status_forcelist=(429, 503),
allowed_methods=frozenset(["GET"]),
respect_retry_after_header=True,
raise_on_status=False,
)
adapter = HTTPAdapter(
max_retries=retry_policy,
pool_connections=10,
pool_maxsize=10,
)
session = Session()
session.mount("https://", adapter)
session.mount("http://", adapter)
return session
def fetch_html(url: str, session: Session) -> str:
response = session.get(
url,
headers=HEADERS,
timeout=20,
)
if response.status_code >= 400:
raise RuntimeError(
f"HTTP {response.status_code} for {url}"
)
html = response.text
lower_html = html.lower()
if "captcha" in lower_html or "robot check" in lower_html:
raise RuntimeError(
"Amazon returned an access-control page. "
"Stop this check and review the access method."
)
return html
def parse_price(text: str) -> Decimal:
cleaned = re.sub(r"[^\d,.\-]", "", text.strip())
if not cleaned:
raise ValueError(f"No numeric price found in {text!r}")
# Handles formats such as:
# 1,299.99
# 1.299,99
# 29,99
# 1,299
if "," in cleaned and "." in cleaned:
decimal_separator = (
"," if cleaned.rfind(",") > cleaned.rfind(".") else "."
)
thousands_separator = (
"." if decimal_separator == "," else ","
)
cleaned = cleaned.replace(thousands_separator, "")
cleaned = cleaned.replace(decimal_separator, ".")
elif "," in cleaned:
parts = cleaned.split(",")
if len(parts[-1]) in (1, 2):
cleaned = "".join(parts[:-1]) + "." + parts[-1]
else:
cleaned = "".join(parts)
elif "." in cleaned:
parts = cleaned.split(".")
if len(parts) > 2 or len(parts[-1]) == 3:
cleaned = "".join(parts)
try:
return Decimal(cleaned)
except InvalidOperation as exc:
raise ValueError(
f"Could not parse price: {text!r}"
) from exc
def scrape_product(
url: str,
session: Session,
) -> dict:
html = fetch_html(url, session)
soup = BeautifulSoup(html, "html.parser")
title_element = soup.select_one("#productTitle")
if title_element is None:
raise RuntimeError(
"Product title was not found."
)
price_element = None
matched_selector = None
for selector in PRICE_SELECTORS:
candidate = soup.select_one(selector)
if candidate and candidate.get_text(strip=True):
price_element = candidate
matched_selector = selector
break
if price_element is None:
raise RuntimeError(
"No main product price was found. "
"The page layout may have changed."
)
return {
"title": title_element.get_text(" ", strip=True),
"price": parse_price(
price_element.get_text(strip=True)
),
"selector": matched_selector,
"url": url,
} Why the User-Agent and Headers Matter
Parsing Price Text Safely with Decimal
from datetime import datetime, timezone
from pathlib import Path
import pandas as pd
def append_history(
product: dict,
asin: str,
currency: str = "USD",
csv_path: str = "price_history.csv",
) -> None:
row = pd.DataFrame(
[
{
"timestamp": datetime.now(timezone.utc).isoformat(),
"asin": asin,
"title": product["title"],
"price": str(product["price"]),
"currency": currency,
"url": product["url"],
"selector": product["selector"],
}
]
)
file_exists = Path(csv_path).exists()
row.to_csv(
csv_path,
mode="a",
header=not file_exists,
index=False,
)
import os
import smtplib
from decimal import Decimal
from email.message import EmailMessage
def send_price_alert(product: dict, target_price: Decimal) -> None:
sender = os.environ["TRACKER_EMAIL"]
app_password = os.environ["TRACKER_EMAIL_APP_PASSWORD"]
recipient = os.environ["TRACKER_ALERT_TO"]
message = EmailMessage()
message["Subject"] = (
f"Price alert: {product['title'][:60]}"
)
message["From"] = sender
message["To"] = recipient
message.set_content(
f"""The tracked Amazon price reached the target.
Product: {product['title']}
Current price: {product['price']}
Target price: {target_price}
URL: {product['url']}
"""
)
with smtplib.SMTP_SSL("smtp.gmail.com", 465) as smtp:
smtp.login(sender, app_password)
smtp.send_message(message)
def check_target(product: dict, target_price: Decimal) -> None:
if product["price"] <= target_price:
send_price_alert(product, target_price) Setting Environment Variables — and Remembering to Re-set Them
import time
from decimal import Decimal
import schedule
PRODUCT_URL = "https://www.amazon.com/dp/YOUR_ASIN_HERE"
ASIN = "YOUR_ASIN"
TARGET_PRICE = Decimal("299.99")
SESSION = build_session()
def run_check():
try:
product = scrape_product(
PRODUCT_URL,
SESSION,
)
append_history(
product,
ASIN,
"USD",
)
check_target(
product,
TARGET_PRICE,
)
print(
f"Checked {ASIN}: "
f"{product['price']} "
f"at {product['selector']}"
)
except Exception as exc:
print(
f"Check failed for {ASIN}: {exc}"
)
if __name__ == "__main__":
run_check()
schedule.every(8).hours.do(run_check)
while True:
schedule.run_pending()
time.sleep(30)
import time
from decimal import Decimal
import schedule
PRODUCT_URL = "https://www.amazon.com/dp/YOUR_ASIN_HERE"
ASIN = "YOUR_ASIN"
TARGET_PRICE = Decimal("299.99")
def run_check():
try:
product = scrape_product(PRODUCT_URL)
append_history(product, ASIN, "USD")
check_target(product, TARGET_PRICE)
print(
f"Checked {ASIN}: "
f"{product['price']} at {product['selector']}"
)
except Exception as exc:
print(f"Check failed for {ASIN}: {exc}")
schedule.every(8).hours.do(run_check)
run_check()
while True:
schedule.run_pending()
time.sleep(30)
Residential vs. Datacenter Proxies
import pandas as pd
import matplotlib.pyplot as plt
history = pd.read_csv(
"price_history.csv",
parse_dates=["timestamp"],
)
history["price"] = pd.to_numeric(
history["price"],
errors="coerce",
)
history = history.dropna(subset=["price"])
history = history.sort_values("timestamp")
current_price = history.iloc[-1]["price"]
lowest_price = history["price"].min()
highest_price = history["price"].max()
average_price = history["price"].mean()
is_lowest = current_price == lowest_price
print(f"Current: {current_price:.2f}")
print(f"Lowest: {lowest_price:.2f}")
print(f"Highest: {highest_price:.2f}")
print(f"Average: {average_price:.2f}")
print(f"At historical low: {is_lowest}")
history.plot(
x="timestamp",
y="price",
legend=False,
)
plt.title("Amazon Price History")
plt.xlabel("Date")
plt.ylabel("Price")
plt.tight_layout()
plt.show()
Why does my Amazon scraper return None for the price?
How often should a Python Amazon price tracker check prices?
Do I need proxies to track Amazon prices?
Can the same Python tracker monitor Amazon.co.uk, Amazon.de, and other marketplaces?
When does a managed price tracking tool make more sense than a custom script?