How to Build an Amazon Price Tracking Bot to know Price Drops

How to Build an Amazon Price Tracking Bot to know Price Drops

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 which 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 along with the libraries such as Requests (to request the webpage), BeautifulSoup(to extract out the information from the webpage), SMTP-LIB(SMTP is a mail protocol which will be used to send mails to the user). 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()

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 🙂


 

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.

Previous How to Find and Reduce Boot Time in Linux
Next How To Create An ISO Image In Linux Using Terminal?

About author

You might also like

Scripting 0 Comments

Script to Make an Instagram Bot to get Followers

Backstory: So last week during my internship one of the work assigned to me was to go and follow some random profiles on Instagram through the company’s page. You might wonder why

Scripting 0 Comments

How to make files Hidden in Linux

Linux provides us the functionality to hide the files for security as per user preference, users can easily hide files by using some simple command or even use GUI for

Scripting 0 Comments

Script to delete files in Linux

Linux is a powerful operating system, it consists of various tools which can be utilised by the user to achieve maximum efficiency in his/her work routine. Linux provides user with

0 Comments

No Comments Yet!

You can be first to comment this post!

Leave a Reply