Hackernoon logoHow to Develop a Price Comparison Tool in Python by@sandra-moraes

How to Develop a Price Comparison Tool in Python

Author profile picture

@sandra-moraesSandra Moraes

Data Scientist

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.
Even with the money exchange services, you can try the comparison tool to know which company is providing more exchange rate. Always go with the trusted money exchange centre to get the best user experience.

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:
    "last_visited": "2018-01-30T13:38:01",
    "name": "PUMA Men's Evospeed 17.4 TT Soccer Shoe",
    "amazon_price": 36.94,
    "ebay_price": 37,
    "walmart_price": 37,
    "amazon_url": "https://www.amazon.com/PUMA-Evospeed-Soccer-Ultra-Yellow-Peacoat-Orange/dp/B01J5LEMZI/",
    "ebay_url": "https://www.ebay.com/itm/PUMA-Mens-Evospeed-17-4-Tt-Soccer-Shoe/302471489090",
    "walmart_url": "https://www.walmart.com/ip/PUMA-Men-s-Evospeed-17-4-Tt-Soccer-Shoe/587074448",
    "description": "The new evospeed 17.4 is a performance football boot for players of all levels. The soft and lightweight synthetic leather on the upper keeps the boot lightweight, comfortable and ensures durability. The lightweight outsole offers the perfect balance between traction, stability and acceleration PUMA is the global athletic brand that successfully fuses influences from sport, lifestyle and fashion. PUMA's unique industry perspective delivers the unexpected in sport-lifestyle footwear, apparel and accessories, through technical innovation and revolutionary design.",
    "brand": "PUMA",
    "image": "https://images-na.ssl-images-amazon.com/images/I/61v1mylcAqL._UL1500_.jpg"
    "last_visited": "2018-01-30T13:38:07",
    "name": "L'Oreal Paris Skin Care Revitalift Cicacream Face Moisturizer",
    "amazon_price": 13.97,
    "ebay_price": 13.99,
    "walmart_price": 13.97,
    "amazon_url": "https://www.amazon.com/LOreal-Paris-Revitalift-Cicacream-Moisturizer/dp/B074MBDRHW",
    "ebay_url": "https://www.ebay.com/itm/LOREAL-Paris-NEW-Revitalift-Cicacream-Anti-Wrinkle-Skin-Barrier-Repair-ORIGINAL/112715734801",
    "walmart_url": "https://www.walmart.com/ip/L-Or-al-Paris-Revitalift-Cicacream-Anti-Wrinkle-Skin-Barrier-Repair/519350834",
    "description": "Skin's moisture barrier weakens with age, resulting in greater moisture loss, more prominent wrinkles and loss of firmness. Lightweight, protective cream is formulated with Pro-Retinol, a powerful wrinkle-fighting ingredient and Centella Asiatica, an herb used in traditional Chinese medicine. Strengthens and repairs skin barrier to help resist visible lines, loss of firmness and other signs of aging that a weakened skin barrier can accentuate. See visible results immediately: skin feels healthier, softer, smoother and more supple. Skin feels noticeably more hydrated. Skin barrier is stronger, helping to resist signs of aging. In two weeks: fine lines appear visibly reduced. Firmness and elasticity look noticeably improved. In four weeks: wrinkles appear less visible. Clarity and tone improves, skin exudes luminosity. Skin continues to look and feel soft, smooth, healthy.",
    "brand": "L'Oreal Paris",
    "image": "https://images-na.ssl-images-amazon.com/images/I/71Ff2vn4vjL._SL1500_.jpg"
    "last_visited": "2018-01-30T13:38:12",
    "name": "Adidas Dynamic Pulse By Adidas For Men",
    "amazon_price": 6.96,
    "ebay_price": 18.99,
    "walmart_price": 7,
    "amazon_url": "https://www.amazon.com/Adidas-Dynamic-Toilette-3-4-Ounce-Bottle/dp/B000VON5F2/",
    "ebay_url": "https://www.ebay.com/itm/Adidas-DYNAMIC-PULSE-Cologne-for-Men-3-4-oz-edt-3-3-Spray-New-in-BOX/252837623533",
    "walmart_url": "https://www.walmart.com/ip/Adidas-Dynamic-Pulse-for-Men-3-4-oz-EDT/28664356",
    "description": "Launched by the design house of Adidas in 1997, ADIDAS DYNAMIC PULSE is a men's fragrance that possesses a blend of A fresh scent of citrus, cedar and mint with low tones of sweet fruits, fragrant woods and tonka bean. It is recommended for daytime wear.When applying any fragrance please consider that there are several factors which can affect the natural smell of your skin and, in turn, the way a scent smells on you. For instance, your mood, stress level, age, body chemistry, diet, and current medications may all alter the scents you wear. Similarly, factor such as dry or oily skin can even affect the amount of time a fragrance will last after being applied",
    "brand": "adidas",
    "image": "https://images-na.ssl-images-amazon.com/images/I/41%2BAnOP5nbL.jpg"
    "last_visited": "2018-01-30T13:38:19",
    "name": "Canon EOS Rebel T6 Digital SLR Camera",
    "amazon_price": 449,
    "ebay_price": 449,
    "walmart_price": 449,
    "amazon_url": "https://www.amazon.com/Canon-Digital-Camera-18-55mm-3-5-5-6/dp/B01CO2JPYS",
    "ebay_url": "https://www.ebay.com/itm/Canon-EOS-Rebel-T6-DSLR-Camera-with-18-55mm-Lens/232596041502",
    "walmart_url": "https://www.walmart.com/ip/Canon-EOS-Rebel-T6-DSLR-Camera-with-18-55mm-Lens-Black/50820749",
    "description": "",
    "brand": "Canon",
    "image": "https://images-na.ssl-images-amazon.com/images/I/81YszfZS8%2BL._SL1500_.jpg"
    "last_visited": "2018-01-30T13:38:25",
    "name": "Woodland Fox Critter 36' Mylar Balloon",
    "amazon_price": 5.49,
    "ebay_price": 6.49,
    "walmart_price": 7.6,
    "amazon_url": "https://www.amazon.com/Woodland-Fox-Critter-Mylar-Balloon/dp/B00S9TKVYO",
    "ebay_url": "https://www.ebay.com/itm/Woodland-Critters-Fox-36-inch-Foil-Balloon/132058119680",
    "walmart_url": "https://www.walmart.com/ip/Woodland-Fox-Foil-Balloon/43350002",
    "description": "Celebrate any occasion with an adorable woodland fox critter balloon! 36\" Woodland Critters fox shape foil balloon.",
    "brand": "Betallic",
    "image": "https://images-na.ssl-images-amazon.com/images/I/71Z9bG-BzuL._SL1500_.jpg"
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.
import json

if __name__ == '__main__':
    price_data = None
    price = []
    with open('data.json', encoding='utf8') as f:
        price_data = f.read()

    if price_data is not None:
        json_price_data = json.loads(price_data)
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 
, and 
 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.
for d in json_price_data:
            price.append({'name': d['name'], 'price': float(d['amazon_price']), 'url': d['amazon_url']})
            price.append({'name': d['name'], 'price': float(d['walmart_price']), 'url': d['walmart_url']})
            price.append({'name': d['name'], 'price': float(d['ebay_price']), 'url': d['ebay_url']})
            minPricedItem = min(price, key=lambda x: x['price'])
            price = []
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:
for d in json_price_data:
            price.append({'name': d['name'], 'price': d['amazon_price'], 'url': d['amazon_url']})
            price.append({'name': d['name'], 'price': d['walmart_price'], 'url': d['walmart_url']})
            price.append({'name': d['name'], 'price': d['ebay_price'], 'url': d['ebay_url']})
            minPricedItem = min(price, key=lambda x: float(x['price']))
            store_name = ''
            # Pick the store name based on url
            if 'amazon' in minPricedItem['url'].lower():
                store_name = 'Amazon'
            elif 'walmart' in minPricedItem['url'].lower():
                store_name = 'Amazon'
            elif 'ebay' in minPricedItem['url'].lower():
                store_name = 'eBay'
            print('{} is available in cheap price at {}. The price is ${}'.format(minPricedItem['name'], store_name,
            price = []
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.


The Noonification banner

Subscribe to get your daily round-up of top tech stories!