Contact information

Odisha, India

Well, It was around mid of 2018 and i was going crazy to buy a new smartphone and it was too costly (Not gonna mention the brand !). So i came up with an idea to build a small python app that tracks the prices and if that price falls down it gonna send me an email notification.

In this blog, I am going to take you through a step by step guided tutorial on Build A Python App That Tracks Online Prices And Sends Email Notifications. Let’s code together. It gonna be fun !

But if you already know the whole thing and just want to take a look at the code. Here you go.

First we are going to install two necessary libraries namely requests and bs4. In short the requests library is going to help us in accessing the web and bs4 is going to help pull the data from the website. So fire up your terminal and type the below command.

pip install requests bs4

Now let’s import the libraries that we installed and another necessary library that we will use for sending e-mail and routing e-mail between mail servers.

import requests
from bs4 import BeautifulSoup
import smtplib

Every time your web browser makes a request to a website, it sends a HTTP Header called the “User Agent”. The User Agent string contains information about your web browser name, operating system, device type and lots of other useful bits of information.In order to know your User Agent just google “My user agent”. You will get something like this.

Now let’s define the URL of the product (For which we are going to track the price) and the user agent.

URL = 'producturl.com/xyz'
headers = {"User-Agent": 'YOUR USER AGENT'}

It’s time to write the function that is going to fulfil the first purpose of our Python app. We are going to define a function check_price() which will include accessing the URL, extract the information of the webpage(which includes the product name and its price) and parse them using BeautifulSoup class. We have also defined a if condition which is going to compare the price and if the comparison gets output as true the bingo ! We are going to receive the email.

def check_price():    
     page= requests.get(URL, headers=headers)     
     soup = BeautifulSoup(page.content, 'html.parser')     
     title = soup.find(id = "ebooksProductTitle").get_text()     
     for price in soup.findAll(attrs={'class' : 'a-size-medium a-color-price'}):
        price = price.text.strip()     
     converted_price =  float(price[0:5])     
     if(converted_price < 179):        
        send_email()     
    

In the above function we have used id as our locator for extracting the title and class for extracting the price of the product. To explore more about BeautifulSoup you can go to their crummy site.

Now we are ready to define the function that is going to fulfill the second purpose of our Python app. We will be defining a function send_email(). But before that we need to generate password for the senders mail as some devices does not support 2 factor authentication. In order to generate that just navigate to Google Account’s Apps Passwords. You will be getting a screen just like the below image.

Select app as “Mail” and Select devices according to your requirement. Click generate ! Well you can clearly see your password now. Now lets’s code up the send_email() function.

def send_email():     
     server = smtplib.SMTP('smtp.gmail.com', 587)    
     server.ehlo()    
     server.starttls()    
     server.ehlo()    
     server.login('sender's email', '#password')     
     subject = 'Write your subject here'     
     body = 'Write your mail content here'     
     msg = f"Subject: {subject} \n\n {body}"
     server.sendmail('sender's email', 'receiver's email', msg)     
     print('Email sent !')     
     server.quit()

So in the above function we are sending mail using Python’s smtplib using an SMTP (Simple Mail Transfer Protocol) server. SMTP object which is going to be used for connection with the server. EHLO command will be used to identify the domain name of the sending host to SMTP before we issue a MAIL. Finally let’s call our check_price() function. Bingo !

check_price()
Share:

administrator

Leave a Reply

Your email address will not be published. Required fields are marked *