Building an Amazon price tracker with Python.
Are you tired of constantly checking Amazon for price drops on your favorite products? In this tutorial, we'll show you how to build an Amazon price tracker using Python. With this tool, you'll be able to set up alerts for when the prices of your desired products change, so you can snag a deal as soon as it becomes available.

Prerequisites
Before we get started, you’ll need to make sure you have the following tools and resources:
- Python 3.x: You can download the latest version of Python from the official website.
- A text editor: You can use any text editor you like, such as Sublime Text or Visual Studio Code.
- An Amazon account: You’ll need an Amazon account to access the product data we’ll be using in this tutorial. If you don’t have an account, you can create one for free on the Amazon website.
Step 1: Set up your environment
First, you’ll need to set up your environment. Open up your text editor and create a new file called price_tracker.py
. This will be the file where we write our Python code.
Next, you’ll need to install the following Python packages:
beautifulsoup4
: A library for parsing and navigating HTML and XML documents.requests
: A library for making HTTP requests.
You can install these packages using pip
, the Python package manager. Open up a terminal window and run the following command:
1pip install beautifulsoup4 requests
This will install the necessary packages for our price tracker.
Step 2: Scrape the product data
Now that our environment is set up, we can start scraping the product data from Amazon. We’ll use the requests
library to send an HTTP request to the Amazon product page and retrieve the HTML content. Then, we’ll use the beautifulsoup4
library to parse the HTML and extract the data we need.
First, let’s import the necessary libraries at the top of our price_tracker.py
file:
1import requests
2from bs4 import BeautifulSoup
Next, let’s define a function called get_product_data
that takes a product URL as an argument and returns a dictionary containing the product data. Inside the function, we’ll use the requests
library to send an HTTP request to the product URL and retrieve the HTML content.
1def get_product_data(url):
2 response = requests.get(url)
3 html = response.text
4 soup = BeautifulSoup(html, 'html.parser')
5 return soup
Now that we have the HTML content of the product page, we can use the beautifulsoup4
library to parse it and extract the data we need. For example, let’s say we want to extract the product title, price, and rating. We can do this by using the find
method to locate the relevant HTML elements and extracting the text from those elements.
1def get_product_data(url):
2 response = requests.get(url)
3 html = response.text
4 soup = BeautifulSoup(html, 'html.parser')
5
6 # Extract product title
7 title_element = soup.find('span', {'id': 'productTitle'})
8 title = title_element.text.strip()
9
10 # Extract product price
11 price_element = soup.find('span', {'class': 'a-price'})
12 price = price_element.text.strip()
13
14 # Extract product rating
15 rating_element = soup.find('span', {'class': 'a-icon-alt'})
16 rating = rating_element.text.strip()
17
18 data = {
19 'title': title,
20 'price': price,
21 'rating': rating
22 }
23 return data
Great! Now that we have a way to extract the product data from the Amazon product page, let’s move on to the next step: storing the data in a database.
Step 3: Store the data in a database
There are many different databases you can use to store your data, such as MySQL, PostgreSQL, and MongoDB. For this tutorial, we’ll use SQLite, a lightweight, file-based database that is included with Python.
First, let’s import the necessary library at the top of our price_tracker.py
file:
1import sqlite3
Next, let’s create a function called create_database
that sets up our SQLite database. Inside the function, we’ll create a connection to the database and create a table to store our product data.
1def create_database():
2 conn = sqlite3.connect('price_tracker.db')
3 c = conn.cursor()
4 c.execute('''CREATE TABLE IF NOT EXISTS products
5 (title text, price text, rating text)''')
6 conn.commit()
7 conn.close()
Now that we have our database set up, let’s create a function called add_product
that inserts a product into the database. This function will take three arguments: the product title, price, and rating.
1def add_product(title, price, rating):
2 conn = sqlite3.connect('price_tracker.db')
3 c = conn.cursor()
4 c.execute("INSERT INTO products VALUES (?, ?, ?)", (title, price, rating))
5 conn.commit()
6 conn.close()
With these functions in place, we can now store our product data in the database.
Step 4: Set up alerts
Now that we have a way to track and store the prices of our desired products, let’s set up a system to alert us when the prices change. There are many ways you could do this, such as sending an email, SMS, or push notification. In this tutorial, we’ll use email notifications.
To send email notifications, we’ll need to use a library called smtplib
, which allows us to send emails using the Simple Mail Transfer Protocol (SMTP). Let’s import this library at the top of our price_tracker.py
file:
1import smtplib
Next, let’s create a function called send_email
that takes three arguments: the recipient’s email address, the subject of the email, and the body of the email. Inside the function, we’ll use the smtplib
library to send the email through an SMTP server.
1def send_email(to, subject, body):
2 server = smtplib.SMTP('smtp.gmail.com', 587)
3 server.starttls()
4 server.login("YOUR_EMAIL_ADDRESS", "YOUR_EMAIL_PASSWORD")
5 msg = f"Subject: {subject}\n\n{body}"
6 server.sendmail("YOUR_EMAIL_ADDRESS", to, msg)
7 server.quit()
Note: In order to use this function, you’ll need to have a Gmail account and allow less secure apps to access your account. You can do this by going to the Security settings in your Google Account.
Now that we have a way to send email notifications, let’s create a function called check_price
that checks the current price of a product and sends an email notification if the price has changed. This function will take three arguments: the product URL, the desired price, and the recipient’s email address.
1def check_price(url, desired_price, recipient_email):
2 product_data = get_product_data(url)
3 current_price = product_data['price']
4
5 if current_price < desired_price:
6 send_email(recipient_email, "Price drop alert!", f"The price of {product_data['title']} has dropped to {current_price}!")
With these functions in place, we now have a fully-functioning Amazon price tracker. To use it, simply call the check_price
function with the desired product URL, desired price, and recipient email address. You can also customize the frequency of the price checks by using a tool like time.sleep
or by setting up a cron job.
Hosting
That’s it! You now have a tool that will alert you as soon as the price of your desired product drops on Amazon. If you want this service to keep running even if your computer is off, you can check this article on How to host a Python Script to run on Cloud. Happy shopping!