Have you ever bought a gadget online, only to check the next day and find the price had already dropped? Manually tracking prices across e-commerce websites is exhausting and simply not practical around the clock. The good news is that Python lets you automate this entire process. In this guide, you will learn how to build a price drop alert bot with Python that watches any product page and sends you a notification the moment the price hits your target.
Automating repetitive tasks is one of the most powerful skills a developer can have. Building a price monitor not only saves you money, it also gives you hands-on practice with HTTP requests, HTML parsing, and automated notifications. This project is a great starting point for anyone looking to learn Python, because it combines real-world usefulness with code that is approachable and easy to follow.
What Is Web Scraping and How Does It Help You Save Money?
The core technique behind any price monitoring bot is Web Scraping. Scraping works by having a script simulate a browser visit to a website, read the page content, and extract specific pieces of information, such as a product name and its current price. Instead of opening a browser and checking the price yourself, the script does it for you on a set schedule.
For this project, Python is the ideal choice because of its rich ecosystem of automation libraries. If you do not have a coding environment ready yet, it is worth checking the guide on installing and setting up VS Code on Windows before moving forward.
Tools You Need to Build Your Bot
To build this project, you will need a few Python libraries that handle web communication and data parsing. Here are the key ones:
- Requests: Sends an HTTP request to the product page and downloads its HTML content.
- BeautifulSoup: Parses the downloaded HTML and lets you search for specific tags, classes, or IDs that contain the price.
- Smtplib: A built-in Python library used to send email notifications automatically when the price reaches your target.
If you get import errors when trying to use these libraries, your interpreter may not be configured correctly or an installation step may have failed. Errors like ModuleNotFoundError are very common for beginners and straightforward to fix by following community best practices.
Setting Up Your Development Environment
1. Installing the Libraries
Open your terminal and run the command below to install the Requests library and BeautifulSoup using pip:
pip install requests beautifulsoup42. Identifying the Right HTML Elements
Before writing any code, visit the product page you want to monitor on a site like Amazon. Right-click on the displayed price and select “Inspect”. Note the HTML ID or class name that wraps the numeric value. This is the “address” your bot will use to find and read the correct data every time it runs.
Writing the Price Capture Logic
In this step, you will write the Python code that visits the product page and reads the price. One important detail is setting a User-Agent header. This tells the website that the request is coming from a regular browser rather than a bot, which helps avoid being blocked immediately.
import requests
from bs4 import BeautifulSoup
def get_price(url):
headers = {"User-Agent": "Your User Agent Here"}
site = requests.get(url, headers=headers)
soup = BeautifulSoup(site.content, 'html.parser')
# Example selector (this changes from site to site)
price = soup.find(id="priceblock_ourprice").get_text()
# Converting price string to a real number (float)
price_number = float(price.replace('$', '').replace(',', '').strip())
return price_numberWhen dealing with currency formatting, you will need to clean up the text string before converting it to a number. Learning how to use f-strings to format numbers and currencies can help you display the final value cleanly in the terminal during testing.
Setting Up the Price Alert
Now that your bot knows the current price, it needs to decide whether to notify you. This is done with a simple conditional check: if the collected price is lower than your target, the bot triggers an action. In this tutorial, the action is sending an email, but you can also create a Telegram bot with Python to receive notifications directly on your phone.
import smtplib
def send_email(link):
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login('[email protected]', 'yourpassword')
subject = 'Price drop alert!'
body = f'Check the deal here: {link}'
msg = f'Subject: {subject}nn{body}'
server.sendmail('[email protected]', '[email protected]', msg)
server.quit()Important: To use Gmail as the sender, you need to generate an “App Password” in your Google account security settings. Google blocks direct script logins for security reasons, so a regular password will not work here.
Automating the Check Frequency
A bot that you have to run manually every time is not very useful. You need it to check the price on its own at regular intervals, for example once every hour. You can achieve this with a while loop and the Python time module to control how long the script pauses between each check.
import time
while True:
current_price = get_price(PRODUCT_URL)
if current_price <= 150.00:
send_email(PRODUCT_URL)
break # Stop the bot after sending the alert
time.sleep(3600) # Wait 60 minutes before checking againWhen running continuous loops, it is important to make sure your script does not consume excessive CPU resources. Reading about Python performance optimization tips can help you keep the script lightweight and efficient, even when it runs for days at a time.
Ethical and Legal Considerations
When building scrapers, it is essential to respect the target website’s robots.txt file. This file specifies which pages bots are allowed to access. Avoid sending requests at very short intervals, such as every few seconds, as this can overload the store’s servers and get your IP address blocked. Respecting the privacy policies and terms of service of major platforms, as outlined by the World Wide Web Consortium (W3C), ensures your automation stays ethical.
For larger and more professional projects, consider using frameworks like Scrapy, which includes advanced tools for managing cookies, sessions, and more complex data pipelines.
Complete Project Code
Here is the full, unified code for your price monitoring bot. Remember to replace the placeholder values with your real information and adjust the HTML selectors to match the specific website you want to monitor.
import requests
from bs4 import BeautifulSoup
import smtplib
import time
# INITIAL SETTINGS
PRODUCT_URL = "INSERT_THE_PRODUCT_URL_HERE"
TARGET_PRICE = 120.00
MY_EMAIL = "[email protected]"
APP_EMAIL_PASSWORD = "yourapppassword" # Use a Google App Password
USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
def check_price():
headers = {"User-Agent": USER_AGENT}
try:
response = requests.get(PRODUCT_URL, headers=headers)
soup = BeautifulSoup(response.content, 'html.parser')
title = soup.find(id="productTitle").get_text().strip()
price_text = soup.find(class_="a-offscreen").get_text()
clean_price = price_text.replace('$', '').replace(',', '').strip()
price = float(clean_price)
print(f"Product: {title}")
print(f"Current price: ${price:.2f}")
if price <= TARGET_PRICE:
send_email(PRODUCT_URL)
return True
return False
except Exception as e:
print(f"Error: {e}")
return False
def send_email(link):
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login(MY_EMAIL, APP_EMAIL_PASSWORD)
subject = 'Price drop alert!'
body = f'The price dropped! Check it here: {link}'
msg = f'Subject: {subject}nn{body}'
server.sendmail(MY_EMAIL, MY_EMAIL, msg)
server.quit()
while True:
if check_price():
break
time.sleep(3600)Frequently Asked Questions
Does the bot work on any e-commerce website?
The programming logic is the same for every website, but the selectors (IDs and class names) are different on each one. You will need to adjust the code for each store you want to monitor.
Why does the bot sometimes get a connection error?
Large websites often have bot protection systems in place. If your script sends too many requests in a short period, your IP address may be temporarily blocked. Always use reasonable time intervals between checks.
Can I get banned from a store for using this bot?
It is unlikely that your buyer account will be banned, but the bot may stop working if the website detects abusive automated traffic. Always use the script responsibly and with moderate request frequency.
How do I find my User-Agent string?
Simply search for “What is my user agent” on Google and it will display the exact string that your current browser is using.
Does the bot require my computer to stay on?
Yes, the script only runs while your computer is on and connected to the internet. To run it 24 hours without interruption, you can host it on a cloud server (VPS) or on a Raspberry Pi.
Can I monitor multiple products at the same time?
Absolutely. You can create a Python list with multiple URLs and use a for loop to iterate through each one inside the while True block.
Is it safe to put my email password directly in the code?
It is not recommended to store real passwords in your source code. Use Google App Passwords instead, and for better security, store your credentials using environment variables.
The website changed its layout and now the bot is broken. What should I do?
This is a common challenge in web scraping. When a website updates its design, the HTML IDs and class names can change too. Simply open the site, inspect the new price element, and update the selector in your code.



