Have you ever missed a good deal on a product just because you didn’t know about the discounts being offered? Is it not good if someone keeps a track of the Amazon discounts? So, in order to prevent this from happening again, I will build a python bot that will track the prices in realtime and will notify you by a mail on your given email address if the price of the product drops to whatever you would like to buy it at!
I will be basically using python with SMTP-LIB to send mail (SMTP is a mail protocol) along with other libraries such as Requests (to request the webpage), BeautifulSoup (to extract out the information from the webpage). The first step is to install all these libraries, most python versions come bundled with Requests, SMTP-LIB so you just need to install BeautifulSoup! Execute the following command in a terminal:
pip install beautifulsoup4
After installing all the libraries, import all the libraries so that they can be used within our script –
import requests from bs4 import BeautifulSoup import smtplib
To check if the libraries are imported correctly, just try to run your program if there’s no error you have successfully imported all the libraries correctly and you can proceed further. Also make sure to get your User-Agent as it will be used in order to communicate with the webpage. You can get it by searching “my user-agent” on google –
Now add this User-Agent to a header field in your python script like this –
headers = {"User-Agent": 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0.1 Safari/605.1.15'}
The next step is to search for your target product and get its URL, in the example, I have considered a Fitbit Versa 2 as the product. Just copy and paste this URL in the URL field in your python script –
URL = 'https://www.amazon.in/Fitbit-FB507BKBK-Smartwatch-Tracking-Included/dp/B07TWFVDWT/ref=lp_19136330031_1_1?srs=19136330031&ie=UTF8&qid=1573614323&sr=8-1'
After completing these steps, now it’s the time to request the price of the product from the URL you have provide. First request the webpage –
page = requests.get(URL, headers=headers)
Now you can use BeautifulSoup to get the name and price of the product, for this you need to get a parameter (like ‘id’ in this example) to find the name tag, price tag. To get these parameters open the URL in your browser and right click on the price and select “inspect element”, you will now get the attributes related to the product like –
Here you can see ‘id’ as “price block_ourprice”, so what you need to do here is to find the price of the product using this given id (the id is static, however, the price differs with time) –
title = soup.find(id='productTitle').get_text() price = soup.find(id='priceblock_ourprice').get_text()
This will allow us to get the price of the product in the realtime and we can set a condition to send us a response if the price is satisfied by our condition
if(price<14000): try: send_mail()[alert color=”green”]We have successfully completed building our bot, we can now implement SMTP to send user a mail, this will be covered in our next post so stay tuned :)[/alert]
Where is the code to build an Amazon Price Tracking Bot?
For those who don’t want to go deep into the actual process but just want it to work, I have provided the code below. Just copy this script to your python environment, replace URL (with your preferred product) and there you go!
import requests from bs4 import BeautifulSoup import smtplib URL = 'https://www.amazon.in/Fitbit-FB507BKBK-Smartwatch-Tracking-Included/dp/B07TWFVDWT/ref=lp_19136330031_1_1?srs=19136330031&ie=UTF8&qid=1573614323&sr=8-1' headers = {"User-Agent": 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0.1 Safari/605.1.15'} page = requests.get(URL, headers=headers) soup = BeautifulSoup(page.content, 'html.parser') title = soup.find(id='productTitle').get_text() price = soup.find(id='priceblock_ourprice').get_text() print(title.strip()) print(price) price = float(price[2:4]) price = price*1000 def send_mail(): print("Sending mail.........") server = smtplib.SMTP('smtp.gmail.com', 587) server.ehlo() server.starttls() server.ehlo() server.login('your_mail', 'password') subject = "AMAZON PRICE UPDATE" +title body = "HEY THERE IS A PRICE UPDATE IN YOUR PRODUCT" +title+ "link" +URL msg = f"Subject: {subject}\n\n{body}" server.sendmail('from_mail','to_mail',msg) server.quit() print("Mail sent successfully") if(price<20000): try: send_mail() except: print("Mail not sent successfully") else: print("Sorry, try again later")
That’s all on building this automated bot to get price drops of products on Amazon. In the past, I have built a bot to get followers on Instagram and few more to come. For more of such awesome useful scripts, you can always follow me.
That is a nice a quick amazon price tracking script. For large scale tracking, on Amazon or other websites, glassit.co provides an enterprise solution which can track the price for any Amazon product, or any online product for that matter. All you do is provide the item’s URL to Glass It and set a target price to control when you get alerted
Amzdealz.net site is very simple to use. You just type in the name of the product you want — or the web address of the item on Amazon — and up pops the item’s price history. Then, if you wish, you can click on “Start Tracking This Product” and get Amzdealz to alert you the next time the product drops below whatever value you set.