top of page
  • Writer's pictureBhagyeshwari Chauhan

How to Develop a Price Comparison Tool in Python

Updated: Feb 12, 2021

Online shopping for various commodities is no more a luxury but has rather become a necessity now. Getting your desired product on your doorstep has made it easier for consumers to shop effortlessly. As a result, several niche e-commerce or generic shopping sites pop up every year.

This trend is not limited to some specific region rather it’s a global phenomenon now, as more and more people are preferring online shopping over visiting outlets due to traffic congestions and ease of purchasing. This is why it’s predicted that by 2021, overall 15.5% of sales will be generated via online websites.


Ever since the e-commerce boom, consumers now equip themselves with the knowledge of best deals from various websites selling the same product. They’ll do so by visiting multiple sites to find the best deal and compare prices to find the one which suits them the best. In such dynamic scenarios, a price comparison tool comes in handy for business, which can be used to compare prices of its rival firms to quote a competitive one.


What is a price comparison tool?

A Price Comparison Tool lets you track the price of a product across different sources thus helping you to keep informed how’s your competitor is doing. It can also help a business to know when the price of a certain product goes up or down than your own target price.


Data Source

For a price monitoring tool to work, it is essential to have a reliable source. Data could be available in any format and from any source. For this post, we are comparing the products prices we are getting from Amazon, eBay, and Walmart. The data is in JSON format. Our sample data looks like the one shown below:

The fields which are relevant to the script are amazon_price,ebay_price, and walmart_price.

Let’s not get worried about how this JSON is being constructed, assume there are some crawlers running to scrape data from these sites and storing in some DB.


Our data is ready, it’s to get into development.


Development of price comparison tool

This tool is written in Python 3.x and we are going to use JSON library to parse JSON and further processing. The tool will be printing the product name and price of the site providing the most lucrative offer. For sake of simplicity, I am fetching JSON data from a text file. In order to parse JSON, we will need to import built-in JSON library.

import json

Now, we have to read the price data, for that, the code snippet calls open() to read JSON content from the file.

Once JSON data is read, we need to convert it into Python’s built-in data structures, for which the code calls json.loads() method that converts a JSON string to dictionary or list of dictionaries, depending on the number of entries.


Since the goal is to find which store is selling the product at the cheapest price, the price info of relevant store is stored in amazon_price,ebay_price, and walmart_price keys.The idea is to find the minimum price along with relevant details like product and store name. We need to iterate the price list items and find the minimum of each product.

We are using lambdas and setting key of min() to make sure price field is being compared. When it runs it produces the following output:


{‘price’: 36.94, ‘url’: ‘https://www.amazon.com/PUMA-Evospeed-Soccer-Ultra-Yellow-Peacoat-Orange/dp/B01J5LEMZI/’, ‘name’: “PUMA Men’s Evospeed 17.4 TT Soccer Shoe”}

=================

{‘price’: 13.97, ‘url’: ‘https://www.amazon.com/LOreal-Paris-Revitalift-Cicacream-Moisturizer/dp/B074MBDRHW’, ‘name’: “L’Oreal Paris Skin Care Revitalift Cicacream Face Moisturizer”}

=================

{‘price’: 6.96, ‘url’: ‘https://www.amazon.com/Adidas-Dynamic-Toilette-3-4-Ounce-Bottle/dp/B000VON5F2/’, ‘name’: ‘Adidas Dynamic Pulse By Adidas For Men’}

=================

{‘price’: 449, ‘url’: ‘https://www.amazon.com/Canon-Digital-Camera-18-55mm-3-5-5-6/dp/B01CO2JPYS’, ‘name’: ‘Canon EOS Rebel T6 Digital SLR Camera’}

=================

{‘price’: 5.49, ‘url’: ‘https://www.amazon.com/Woodland-Fox-Critter-Mylar-Balloon/dp/B00S9TKVYO’, ‘name’: “Woodland Fox Critter 36′ Mylar Balloon”}


This seems to be working but it is not in a representable format. Let’s change it a bit:

And now when you run it prints the output like below:

PUMA Men’s Evospeed 17.4 TT Soccer Shoe is available in cheap price at Amazon. The price is $36.94


L’Oreal Paris Skin Care Revitalift Cicacream Face Moisturizer is available in cheap price at Amazon. The price is $13.97


Adidas Dynamic Pulse By Adidas For Men is available in cheap price at Amazon. The price is $6.96


Canon EOS Rebel T6 Digital SLR Camera is available in cheap price at Amazon. The price is $449


Woodland Fox Critter 36′ Mylar Balloon is available in cheap price at Amazon. The price is $5.49

That’s it! You can run this script periodically to always have an updated price of the product.


Conclusion

In this blog post, we have shared how you can use Python to make a handy price comparison tool for multiple products. You may go to another step and can come up with a web interface to display similar information.


To know more about how to scrape retail information from e-commerce sites, contact us at Datahut, your big data experts.


9,738 views0 comments

Do you want to offload the dull, complex, and labour-intensive web scraping task to an expert?

bottom of page