Getting Amazon Price Drop alert using this Python script | by Vishal Sharma | Towards Data Science

Python does wonderful things. And, there is hardly anything this beautiful language can’t do. Bundles of libraries have boosted this language to outshine every other when it comes to data analysis, web scraping, or task automation.
Just imagine, you want to buy a new iPhone. You are sick of checking Amazon website again and again. But, it’s price isn’t dropping and you always get disappointed.
What if, there is an automatic price checker that will give you an email alert if there is a price drop. Exciting, isn’t it? Let’s do the same by writing a Python script.
Library Ingredients
import smtplibimport timeimport requests as rqfrom bs4 import BeautifulSoup
There are mainly three libraries that will do the job for us. Requests library will communicate with HTTP server, Beautiful Soup will communicate with the webpage and extract the web elements, and smtplib will send you email alerts.
Meat Code
site = "https://www.amazon.in/Apple-iPhone-11-64GB-Green/dp/B07XVKBY68/ref=sr_1_7?keywords=iphone+11&qid=1573668357&sr=8-7"header = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36'}
First things first, I have requested the web server the Amazon’s iPhone webpage. Then, I created a soup object which parses the webpage. We only want the price of the product. So, using the “select” method, we can get the price of the product.
def get_price():    html = rq.get(site, headers=header).text    soup = BeautifulSoup(html, 'html.parser')    price = [i.get_text() for i in             soup.find_all('span', {'class': 'a-size-medium a-color-price priceBlockBuyingPriceString'})]    final_price = ''.join(price)[2:8]    final_price = int(final_price.replace(',', ''))    if final_price < 64900:        send_email()
Now, whenever the “new price” becomes less than the “scraped price”. An email must be sent to the user regarding the price drop.
We will be using smtplib library to send emails to the user. First, we will create an SMTP object to communicate with the domain’s SMTP server.
Once all is done and set, we are good to log into our email account. And, then send the email to the recipient.
def send_email():    server = smtplib.SMTP('smtp.gmail.com', 587)    server.ehlo()    server.starttls()    server.ehlo()    server.login('Your email address', 'Application-specific password')    subject = "Price fell down"    body = "Check this link: https://www.amazon.in/Apple-iPhone-11-64GB-Green/dp/B07XVKBY68/ref=sr_1_7?keywords=iphone+11&qid=1573668357&sr=8-7"    msg = f"Subject:{subject}\n\n{body}"    server.sendmail('Sender's email', 'Recipient email', msg)    print("Hey, email has been sent!")    server.quit()
NOTE: Parameters might change in smtplib.SMTP() depending on the domain’s SMTP server. If you have Gmail account, you will have to generate Application-specific password for your email address. Else, you will get an Application-specific password required error message when your program tries to log in.
while True:    get_price()    time.sleep(60)
These 40–50 lines of code will do the job for you. Next time, you don’t have to check the website again and again for price drops. Make programs like this for other e-commerce website and save your time and effort.
For discussions and queries, contact me on Linkedin!