Last Updated on October 22, 2021 by Jay
This is part 1 of the Amazon Price Drop Notification Bot series. The name says it all. Stay tuned for future updates if you are interested in making your own bot. In fact, after learning the techniques from this tutorial, you’ll be able to make a price drop notification bot for any online shopping site.
The idea of building this bot came from a personal need – as you know that I recently started adding video tutorials so I need a more efficient mouse for video editing. The mouse I was hoping to buy is selling for $130 on Amazon, pretty expensive and I’m nowhere in a rush of getting it immediately. So I thought, why not wait for a sale to get it? But I need to know when the sale happens so I don’t miss it.
Of course, checking the Amazon site every day is one of the options to catch the sale, but we can do better than that because we know Python! ????
**Disclaimer – the Amazon link included in the tutorial is just a regular link and I don’t earn any commission from it. If you find the tutorial helpful and want to support my work, share the article with your friends, or like the video and subscribe to my Youtube channel. ????
Amazon Price Drop Notification Bot Workflow
The workflow is fairly simple:
- Go to a product page (Amazon), check price.
- If price is lower than the regular price, send a notification to myself. Our choice of communication is 1) email and 2) Whatsapp
- Do the above everyday.
Tools we need
- Selenium & ChromeDriver – browser automation
smtplib– for creating and sending email
We only need to install Selenium.
pip install selenium
You can also use Firefox or Internet Explorer instead of Chrome, but you will have to download different web drivers. We’ll use Chrome Browser & Chrome driver for this tutorial.
- Find your Chrome Browser version. Go to Settings -> About Chrome. It shows I’m using version 94.0.4606.71
2. Goto https://chromedriver.chromium.org/downloads and download the driver that matches your Chrome Brower version. So I’m going to download the ChromeDriver 94.0.4606.61. The Chrome Driver version (v94) must match your Chrome Browser version, otherwise, you will see an error message when using Selenium. The download is a zip file, unzip it and save the chromedriver.exe to a folder of your choice.
Extract data from Amazon (or any website)
Selenium is a tool for web browser automation. Whatever actions we can do manually on a browser (input from the keyboard, mouse click, scrolling, etc.), Selenium can replicate and automate them.
from selenium import webdriver driver = webdriver.Chrome(r'C:\Users\jay\Desktop\PythonInOffice\amazon_price_alert_bot\chromedriver.exe') url = r'https://www.amazon.ca/Logitech-Master-Advanced-Wireless-Mouse/dp/B07S395RWD/ref=sr_1_4?dchild=1&keywords=mouse%2Bmx3&qid=1634182753&sr=8-4&th=1' driver.get(url)
In the above code, we create a
webdriver object and specify that we want to use the Chrome webdriver, pass in the chromedriver.exe file location that we just downloaded.
url can be any working website address, here we use the Amazon product page for the mouse I’m interested in.
driver.get(url) will open up a Chrome Browser and take us to the Amazon page. Don’t close the black screen -i.e. the chromedriver.exe application. Closing it will disconnect our control of the browser.
HTML & finding browser elements
Everything we see in a browser is coded in a language called HTML that uses “markup” or “tag” to annotate all elements for display in a web browser. This means that if we need to find any element in the browser, we just need to know its associated tags.
Now go to the browser page that just popped up after running the above Python code. Use your mouse to select the price “$129.99”, right-click -> Inspect will bring up the Chrome developer tool. We can see all HTML tags for the site with this tool.
Look at the highlighted line in your Chrome Dev tool, which should highlight the $129.99 line. The two things around the $129.99, <span id =….> and </span> are the “tags”. The tag contains information that we can use to find this price.
Finding element by tag id
The id is whatever
id="" in the HTML code, in this case id is “priceblock_ourprice”. Note the
.text attribute at the end, without the attribute, we only get a Web Element object, but we are more interested in the data contained in that object, which can be extracted using the
price = driver.find_element_by_id('priceblock_ourprice').text
Finding element by XPath
Another way to find web elements in Selenium is to use XPath. To get XPath, right-click on the highlighted line the Chrome Dev Tool, Copy -> Copy XPath. Once we have the XPath, we can use the
.find_element_by_xpath() method to extract the information.
price = driver.find_element_by_xpath('//*[@id="priceblock_ourprice"]').text
Of course, there are other ways to find elements. However, depending on the site you are visiting, not all methods will work. For example, some tags don’t have an
id, then we can’t use the
find_element_by_id method. Start by observing what the tags have to offer us, then test each method to find a working solution. I find that XPath is a pretty reliable way.
Let’s also find the product name so we know what product this price is for. Select the name and Inspect, we can find the XPath for the product name.
name = driver.find_element_by_xpath('//*[@id="productTitle"]').text
Now we have the price, but it’s in a text format, note the ‘$129.99’. Since we can expect all price tags to come with a currency sign in front of the number, we just need another tweak to convert the text into a number. In Python, a
string is an iterable, with each character being the individual element. We can use the square bracket notation to select a substring, then use
float() to convert a number-looking string to an actual number.
price = float(price[1:])
With only a few lines of code, we just made a bot that can notify us of a price drop on an Amazon product. I’ve tested the below code on a few other Amazon items and it seems that Amazon site uses the same template for most (if not all) of their product pages. It means if you want to find price information for another product on Amazon, simply replace the URL. It means the below shortcode can help you get information for millions of products on Amazon, that’s the kind of scalability Python is capable of!
from selenium import webdriver driver = webdriver.Chrome(r'C:\Users\jay\Desktop\PythonInOffice\amazon_price_alert_bot\chromedriver.exe') url = r'https://www.amazon.ca/Logitech-Master-Advanced-Wireless-Mouse/dp/B07S395RWD/ref=sr_1_3?dchild=1&keywords=mouse%2Bmx%2Bmaster%2B3&qid=1634060642&s=electronics&sr=1-3&th=1' driver.get(url) name = driver.find_element_by_xpath('//*[@id="productTitle"]').text price = driver.find_element_by_xpath('//*[@id="priceblock_ourprice"]').text price = float(price[1:])
We have completed Step 1 of the series. In the next tutorial, we’ll talk about how to send the information to our email and/or Whatsapp. Stay tuned!