Tutorials  Articles  Notifications  Login  Signup


RK

Rajan Kumar

Founder at HackersFriend Updated Jan. 5, 2020, 7:21 p.m. ⋅ 3761 views

Amazon Product Price tracking using Python


Python has powerful libararies, that can do a lot of things in just a few lines of codes.

In this article I'll show you how you can build a program that can track price of any amazon product and let you know if it's price has dropped than a certain value. Let's get started.

Approach

Idea here is to load amazon's product page and parse the value of price from html elements of page. Then compare that value with the given value. If it is lower, tell user that price has dropped else, tell price is higher.

 

Requirements

Let's implement this approach. For information, I'll use following Python Packages:

  • bs4
  • requests

If you have these packages already installed you are good to go. Otherwise, you can simply install these pckages using follwing command:

pip install package-name

You need to replace package-name with the name of package you want to install.

Once you are done installing all the required packages, you can continue further.

 

Implementation

Let's code our idea.

import requests 
from bs4 import BeautifulSoup
from datetime import datetime
import time



def Amazon_handler(): 
	now = datetime.now()
	current_time = now.strftime("%H:%M:%S")
	print("Time: ", current_time)
	print("Connecting to amazon...")
	URL = "https://www.amazon.in/Oppo-Aurora-Blue-64GB-Storage/dp/B07PQ7CXLV/"
    # Get url to extract data
	r = requests.get(URL) 
	soup = BeautifulSoup(r.content, 'html5lib') 
	print("Fetching price...")
	js_test = soup.find('span', id ='priceblock_ourprice') 
	if js_test is None: 
		js_test = soup.find('span', id ='priceblock_dealprice')		 
	str = "" 
	for line in js_test.stripped_strings : 
		str = line 

	# To integer
	str = str[1:]
	# asc_str = str.encode("ascii")
	str = str.replace(",", "")
	str = str.replace(" ", "")
	current_price = int(float(str)) 
	print("Current price = ", current_price) 
	


while True: 
	Amazon_handler()
	print("Will fetch again after 1 minute. To stop press ctl + c...")
	time.sleep(60) 

 

Time:  18:39:47
Connecting to amazon...
Fetching price...
Current price =  15990
Will fetch again after 1 minute. To stop press ctl + c...
Time:  18:40:50
Connecting to amazon...
Fetching price...
Current price =  15990
Will fetch again after 1 minute. To stop press ctl + c...

 



HackerFriend Logo

Join the community of 1 Lakh+ Developers

Create a free account and get access to tutorials, jobs, hackathons, developer events and neatly written articles.


Create a free account