Rajan Kumar

Rajan Kumar published an article

3 months, 4 weeks ago

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
  • sys
  • schedule
  • time
  • urllib
  • PyQt5

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.

# Amazon product price tracking using Python 
# Get all libs
import bs4 as bs 
import sys 
import schedule 
import time 
import urllib.request 
from PyQt5.QtWebEngineWidgets import QWebEnginePage 
from PyQt5.QtWidgets import QApplication 
from PyQt5.QtCore import QUrl 


class HTMLPage(QWebEnginePage): 

	def __init__(self, url): 
		self.app = QApplication(sys.argv) 
		QWebEnginePage.__init__(self) 
		self.html = '' 
		self.loadFinished.connect(self._on_load_finished) 
		self.load(QUrl(url)) 
		self.app.exec_() 

	def _on_load_finished(self): 
		self.html = self.toHtml(self.Callable) 
		print('Load Done.') 

	def Callable(self, html_str): 
		self.html = html_str 
		self.app.quit() 

def Amazon_exact_url(url): 
	index = url.find("B0") 
	index = index + 10
	current_url = "" 
	current_url = url[:index] 
	return current_url 
	

def Amazon_handler(): 
	url = "https://www.amazon.in/Oppo-Aurora-Blue-64GB-Storage/dp/B07PQ7CXLV/"
    # Get url to extract data
	exacturl = Amazon_exact_url(url) 
	ProductPage = HTMLPage(exacturl) 
	soup = bs.BeautifulSoup(ProductPage.html, 'html.parser') 
	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.replace(", ", "") 
	current_price = int(float(str)) 
	our_price = 20000
	if current_price < our_price : 
		print("Price Dropped. Get the deal")
	else: 
		print("Price is high. Bad Deal") 
	
def TrackerJob(): 
	print("Working....")	 
	Amazon_handler() 



# main Function 
# Here I am checking price every 10 minutes, you can set your own time
schedule.every(10).minutes.do(TrackerJob)

while True: 
	schedule.run_pending() 
	time.sleep(1) 

 

Working...
Load Done.
Price Dropped. Get the deal

 


arrow_upward Upvote

comment Comment

arrow_downward downvote




Go back to feed

HackersFriend Updates


update Aug. 15, 2019, 1:47 p.m.

Full view page of article has been changed to match style of homepage feed. open_in_new


update Aug. 14, 2019, 12:52 a.m.

Total number posts on homepage is incresed to 10 by default and text-decoration of link to post and author is changed. open_in_new


update Aug. 7, 2019, 1:51 a.m.

We changed our homepage. Now, you can see newly published articles, directly on home page. open_in_new


View more


Events


event Nov. 28, 2018, 5:30 p.m.

Python from zero to hero

place Delhi

View details



event Aug. 13, 2018, 5:30 p.m.

Python from zero to hero

place Bangalore ( HackersFriend office BTM Layout)

View details