Flipkart Private Limited is an Indian e-commerce company. It sells many products and the prices of these products keep changing. Generally, during sales and festivals, the price of products drops, and our aim is to buy any product at the lowest possible price.
In this article, we will learn to build a Flipkart product price tracker using python to save money and buy products at the lowest possible price.
Approach
We will continuously check the price of our product using python and once the price hits our target price, we will inform the user.
As we are going to scrape prices from the webpage so we need to find an id or unique class to scrape the price. For this, we will go to the browser and open any product on Flipkart then right-click on price, then select inspect to get the required information.
We got two classes(_30jeq3, _16Jk6d), and we need to select the class that appears once on the whole webpage. For this, we will use python.
Libraries Required
Python libraries make it very easy for us to handle the data and perform typical and complex tasks with a single line of code.
- Requests – Requests library is of an integral part of Python for making HTTP requests to a specified URL.
- BeautifulSoup – It is a Python library for pulling data out of HTML and XML files.
- Time Module – As the name suggests Python time module allows one to work with time in Python.
Python3
import requests product_url = "https: / / www.flipkart.com / lenovo - ideapad - 3 - core - i3 - 11th - gen\ - 8 - gb - 512 - gb - ssd - windows - 11 - home - 82h801l7in - 82h802fjin - \ 82h802l3in - 82h801lhin - thin - light - laptop / p / itm0e009f57a591b\ ?pid = COMG9VHHG6Q3RRJX&lid = LSTCOMG9VHHG6Q3RRJXQHPK6Q&marketplace\ = FLIPKART&q = laptop&store = 6bo % 2Fb5g &srno = s_1_5&otracker = search\ &otracker1 = search&fm = productRecommendation % 2FattachForSwatchProducts \ &iid = 66282f34 - b708 - 4905 - b834 - af51e372d5c5.COMG9VHHG6Q3RRJX.SEARCH&ppt\ = pp&ppn = pp&ssid = q2pdky02pc0000001668894808794&qH = 312f91285e048e09 " # fetch webpage r = requests.get(product_url) # get text of webpage content = r.text # class="_30jeq3 _16Jk6d" print (f "Count of class _30jeq3 is {content.count('_30jeq3')}" ) print (f "Count of class _16Jk6d is {content.count('_16Jk6d')}" ) |
Output:
Count of class _30jeq3 is 4 Count of class _16Jk6d is 1
So, We will use class “_16Jk6d” to extract the price.
Complete code to track Flipkart product price tracker using Python
Define the URL of the product and target price. Define the check_price function that checks the price of the product and return price. Inside the check_price function, we use requests.get() to get the content of the webpage then we use BeautifulSoup to parse HTML. And then we use soup.find() to extract the price from the content of the webpage. Finally, we remove the Rs symbol and comma from it and convert it to an integer. We call the check_price() function and compare the current price with the target price again and again until the current price hits the target price. Once the current price hits the target price, we inform the user and exit the program.
Python3
import requests from bs4 import BeautifulSoup import time product_url = "https: / / www.flipkart.com / lenovo - ideapad - 3 - core - i3 - 11th - gen\ - 8 - gb - 512 - gb - ssd - windows - 11 - home - 82h801l7in - 82h802fjin - \ 82h802l3in - 82h801lhin - thin - light - laptop / p / itm0e009f57a591b\ ?pid = COMG9VHHG6Q3RRJX&lid = LSTCOMG9VHHG6Q3RRJXQHPK6Q&marketplace\ = FLIPKART&q = laptop&store = 6bo % 2Fb5g &srno = s_1_5&otracker = search\ &otracker1 = search&fm = productRecommendation % 2FattachForSwatchProducts \ &iid = 66282f34 - b708 - 4905 - b834 - af51e372d5c5.COMG9VHHG6Q3RRJX.SEARCH&ppt\ = pp&ppn = pp&ssid = q2pdky02pc0000001668894808794&qH = 312f91285e048e09 " target_price = 35000 def check_price(): # fetch webpage r = requests.get(product_url) # parse the html soup = BeautifulSoup(r.content, 'html5lib' ) # extract price using class '_16Jk6d' price = soup.find( 'div' , attrs = { "class" : "_16Jk6d" }).text # remove Rs symbol from price price_without_Rs = price[ 1 :] # remove commas from price price_without_comma = price_without_Rs.replace( "," , "") # convert price from string to int int_price = int (price_without_comma) return int_price cur_price = check_price() print (f "Current price is {cur_price}" ) print ( "We will inform you, once price of product hits out target price" ) print ( "Waiting..." ) while True : # get current price cur_price = check_price() if cur_price < = target_price: print (f "Its time to buy product, its current price is {cur_price}" ) break # wait for 1 minute to check again time.sleep( 60 ) |
Output:
Current price is 38799 We will inform you, once price of product hits out target price Waiting...