Apr-23-2022, 04:00 PM
(This post was last modified: Apr-24-2022, 11:09 AM by Yoriz.
Edit Reason: Added code tags
)
I need to write a program to acquire, extract and compare the price from these two suppliers, for a given product.
There are two online supermarkets which provide price of various products.
Example: The price of coconut from the two different suppliers can be found on the following web pages.
laughs_coconut = 'https://www.laugfssuper.com/index.php/coconut-105320.html'
glomark_coconut = 'https://glomark.lk/coconut/p/11624'
The function def compare_prices(product_laughs,product_glomark) will take in two similar products from two suppliers, and compare the prices to recommend which option is cheaper on a given instance of time.
Example expected output:
Laughs COCONUT - Item#mr-2058 Rs.: 89.0
Glomark Coconut Rs.: 86.0
Glomark is cheaper: 3.0
Here is the demo Code:
There are two online supermarkets which provide price of various products.
Example: The price of coconut from the two different suppliers can be found on the following web pages.
laughs_coconut = 'https://www.laugfssuper.com/index.php/coconut-105320.html'
glomark_coconut = 'https://glomark.lk/coconut/p/11624'
The function def compare_prices(product_laughs,product_glomark) will take in two similar products from two suppliers, and compare the prices to recommend which option is cheaper on a given instance of time.
Example expected output:
Laughs COCONUT - Item#mr-2058 Rs.: 89.0
Glomark Coconut Rs.: 86.0
Glomark is cheaper: 3.0
Here is the demo Code:
import requests import json import sys sys.path.insert(0,'bs4.zip') from bs4 import BeautifulSoup #Imitate the Mozilla browser. user_agent = {'User-agent': 'Mozilla/5.0'} def compare_prices(product_laughs,product_glomark): #TODO: Aquire the web pages which contain product Price #TODO: LaughsSuper supermarket website provides the price in a span text. #TODO: Glomark supermarket website provides the data in jason format in an inline script. #You can use the json module to extract only the price #TODO: Parse the values as floats, and print them. print('Laughs ',product_name_laughs,'Rs.: ' , price_laughs) print('Glomark ',product_name_glomark,'Rs.: ' , price_glomark) if(price_laughs>price_glomark): print('Glomark is cheaper Rs.:',price_laughs - price_glomark) elif(price_laughs<price_glomark): print('Laughs is cheaper Rs.:',price_glomark - price_laughs) else: print('Price is the same')