There are dozens of extensions available for browsers that will act as a price tracker for Amazon products, but they do cost us something; they often add their affiliate links without even informing us or do not work at all.
Moreover, for some extensions, we may need to purchase a plan even for a single tracking instance.
As developers, we can easily solve this simple problem. In fact, we will track the prices on a wishlist, so we can easily add or remove new products without manually adding each one to the code.
Today we will do the same using Python; we are going to write a quick script to help us achieve this. This script will later be able to notify us via the Discord webhook, email, or any other service.
In the past I have built the different types of scrapers using Selenium headless, but for today’s editorial we will be using Playwright to write a scraper for us and notify us later using Discord’s webhook.
The reason to choose Playwright is because it is a modern library, and it is comparatively more efficient and provides higher performance than Selenium or other libraries available out there.
So for a quick tutorial, we will be using the following tech stack:
- Python as a programming language
- Playwright as a scarping library
- SQLite for quick DB to store past prices (not recommended for live projects)
- Discord for live notifications
For the final code, check out the public GitHub repository: https://github.com/namankhare/py-amazon-watchlist-price-tracker
To start with the scraping, first create your wishlist on Amazon; make sure to make it public so your scraper can easily access it without any login hassle.
Now the good thing is Amazon has data attributes from which we can directly fetch prices for an item; we do not even need to format the prices to compare them. Also, we will have the unique product ID for ourselves.
Quick look at how our database table is going to look:
CREATE TABLE IF NOT EXISTS products (
item_id TEXT PRIMARY KEY,
name TEXT NOT NULL,
current_price REAL,
current_date TEXT,
lowest_price REAL,
lowest_date TEXT,
url TEXT
)We will cleverly use the HTML data attributes that we can obtain directly from the Amazon website to scrape the data. As you can see, we are using the attributes here rather than the HTML text. The reason for the distinction is rather straightforward: in order to compare product prices using HTML text, we must format and extract the text when we could have directly obtained the price as a number from the data attributes.
The code snippet that carries out the primary logic is shown below.
items = []
product_elements = await page.query_selector_all("li.g-item-sortable")
print(f"Found {len(product_elements)} potential items.")
for element in product_elements:
try:
# Extract Item ID and Price from data attributes
item_id = await element.get_attribute("data-itemid")
price_str = await element.get_attribute("data-price")
# Extract name and URL from the nested <a> tag using title attribute or text
name = "Unknown"
url = "Unknown"
name_link = await element.query_selector("h2 a.a-link-normal")
if name_link:
name = await name_link.get_attribute("title")
if not name:
name = await name_link.inner_text()
name = name.strip()
href = await name_link.get_attribute("href")
if href:
# Prepend amazon domain if it's a relative URL
if href.startswith('/'):
url = f"https://www.amazon.in{href}"
else:
url = href
# Handle missing item_id by looking for ASIN in the link if not already found
if not item_id or item_id == "null":
if url != "Unknown":
asin_match = re.search(r'/dp/([A-Z0-9]{10})', url)
if asin_match:
item_id = asin_match.group(1)
# Clean price
price = 0.0
if price_str and price_str != "null":
price = float(price_str)
if item_id:
items.append({
"item_id": item_id,
"name": name,
"price": price,
"url": url
})
except Exception as e:
print(f"Error parsing item: {e}")
continue
await browser.close()
return itemsOnce that’s done, we can add a sleep method that will refetch the data again to match and send the notification if the prices have changed. We have defined the interval in seconds in our config.py file.
await asyncio.sleep(CHECK_INTERVAL_SECONDS)It will look for a price change and will notify you if the price have increased or decreased
That’s it; you have built yourself an Amazon price tracker, which will be helpful, especially during Amazon sales. In the future you can directly add new products to this wishlist, and your code will automatically start tracking them instead of relying on adding them manually.
Check out the complete instructions to run this program locally on your system and the complete codebase on GitHub.
Leave a Reply