Build Flipkart Price Tracker using Python

Mukund Madhav
Analytics Vidhya
Published in
5 min readFeb 6, 2020

You are poor like me and can’t afford to buy the new iPhone at their real price. What do you do? You wait for the price drop. (Truth be told, still poor to but the iPhone with the price drop, but we’ll assume I can buy if the price drops 😅 ) Sure enough, you can use one of the thousands of already existing price drop aggregators but how cool would it be to build one on your own? So let’s do that.

First, let us import the packages that we are going to be needing.

requests : Will use to fetch HTML code of web pages
smtplib: Will use to send emails
BeautifulSoup: Will use to parse websites
time: To continuously check for price drop

import requests 
import smtplib
from bs4 import BeautifulSoup
import time

Now that we have imported the packages, it’s time to get the product we’ll want to check the price of continuously.

For this example, I’ll be using the iPhone 11. You can choose to use any other product. Once you have opened the product page copy the web address and store it in a variable as below.

URL = 'https://www.flipkart.com/apple-iphone-11-black-64-gb/p/itm0f37c2240b217?pid=MOBFKCTSVZAXUHGR&lid=LSTMOBFKCTSVZAXUHGREPBFGI'

Now, we’ll want to define the user-agent for the header in the HTTP request we do. User-Agent are a means to identify Operating System and the browser of the agent making the request.

If you want to know what’s your user-agent simply google — “my user agent”

Now copy the user agent and add in a dictionary as below:

headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36' }

We’ll first fetch the webpage with the provided URL and header and store it in a variable.

page = requests.get(URL, headers=headers)

We’ll then use beautiful soup to parse the HTML extracted and stored in the variable ‘page’.

soup = BeautifulSoup(page.content, 'html.parser')

Let’s try to get the title of the product. Open Inspect Elements with Ctrl + Shift + I and point to the product name, you’ll see it’s enclosed in a <span/> tag with a class name, _35KyD6.

The code will look something like,

title = soup.find("span", {"class": "_35KyD6"}).get_text()

We can follow a similar path to extract price.

Replace is used to remove the commas that come along with the price. We use [1:] to get the price without the ‘₹’ symbol.

price = float(soup.find("div", {"class": "_1vC4OE _3qQ9m1"}).get_text()[1:].replace(',',''))

Let us assume we want the email sent if price falls below 55,000. Now let’s enclose everything we have written above to our check_price() function that will check the price of the product on Flipkart.

def check_price():
page = requests.get(URL, headers=headers)
soup = BeautifulSoup(page.content, 'html.parser')

title = soup.find("span", {"class": "_35KyD6"}).get_text()
price = float(soup.find("div", {"class": "_1vC4OE _3qQ9m1"}).get_text()[1:].replace(',',''))

print(price)

if( price < 55000.0 ):
sendmail()

Time to define the set-up the sendmail() function that will be responsible for sending the email when price fall occurs.

To begin, we will define an SMTP client session with the name of the host and the port number. Since we’ll be using Gmail for sending emails the hostname will be smtp.gmail.com with port number as 587.

server = smtplib.SMTP('smtp.gmail.com', 587)

Then we’ll send an ehlo() request. Ehlo is basically meant by which servers identify the client. After the request, we’ll start an SMTP connection in TLS and re-send ehlo().

server.ehlo() 
server.starttls()
server.ehlo()

Now, you’ll have to get app password for Gmail to actually send an email. To do that, go to https://myaccount.google.com/security then click on app passwords

Then choose mail and the device you are in, then generate the password.

After that, save the password and your email as mentioned in the code below.

server.login('your_email@gmail.com', 'your_app_password')

Then let’s define the various aspects of the email, like subject an the message body before sending the email.

subject = 'Hey! Price fell down'
body = 'Check the link ' + URL
msg = f"Subject: {subject}\n\n{body}"
server.sendmail('sender_email@gmail.com', 'receiver_email@gmail.com', msg)

Now let’s enclose everything we have written above to our sendmail() function that will allow for automatic email send when the price falls.

def sendmail():
'''Function called when the email needs to be sent '''
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.ehlo()

server.login('xyz@example.com', 'myAppPassword')

subject = 'Hey! Price fell down'
body = 'Check the link ' + URL

msg = f"Subject: {subject}\n\n{body}"

server.sendmail('sender_email@gmail.com', 'receiver_email@gmail.com', msg)

print('Email Sent')

server.quit()

All done. Except…Automatic checking of price every hour (60*60 seconds). To do that, we’ll take help of sleep function.

while(True):
check_price()
time.sleep(60*60)

You can check the project on Github with the entire python file.

What’s next?

You can use the same to automatically save this data to a Database and make your own price tracker website or export it to a CSV file for a list of products.

--

--