Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 03be8a8

Browse files
Merge pull request avinashkranjan#637 from aashishah/price-comp
Price Comparison and Checker
2 parents 4cfcbdb + 6abc756 commit 03be8a8

File tree

3 files changed

+133
-0
lines changed

3 files changed

+133
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Price Comparison and Checker
2+
3+
- This python script can automatically search for an item across Amazon, Flipkart and other online shopping websites and after performing comparisons, find the cheapest price available.
4+
5+
## What the script does
6+
7+
- User can enter the item to be searched for.
8+
- The script displays a table of five matches in ascending order of prices, for each website in the form of Product Name - Price.
9+
- It also displays the best/cheapest price that is available.
10+
11+
## Setup instructions
12+
13+
- Install the modules listed in Requirements.txt
14+
- Ensure you have Python installed in your system.
15+
- Simply run the program titled "price_comparison.py" or execute on Command Prompt/Terminal.
16+
- Next, You will be prompted to enter the item to be searched for and upon doing so, the program will list 5 products from all available websites (currently Amazon, Flipkart and Snapdeal) and give you the best price from all those available.
17+
18+
## Output
19+
![screenshot](https://user-images.githubusercontent.com/49470807/111500289-d6344e00-8769-11eb-99ea-bf24db4e1b41.PNG)
20+
21+
## Author(s)
22+
23+
## [Aashi Shah](https://github.com/aashishah)
24+
25+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
- bs4
2+
- tabulate
3+
- requests
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import requests
2+
from bs4 import BeautifulSoup
3+
from tabulate import tabulate
4+
5+
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36"}
6+
7+
8+
#MAIN WEBSITE SCRAPERS
9+
10+
def amazon(item):
11+
URL = "https://www.amazon.in/s?k=" + item.replace(" ", "+")
12+
page = requests.get(URL, headers = headers)
13+
soup = BeautifulSoup(page.content, "html.parser")
14+
name = "Amazon"
15+
#For access to product links un-comment these:
16+
#links = []
17+
#for link in soup.find_all("a",{"class":"a-link-normal a-text-normal"}, limit = 5):
18+
#links.append(link["href"])
19+
20+
products = []
21+
prices = []
22+
23+
for sp in soup.find_all("span", {"class": ["a-size-base-plus a-color-base a-text-normal", "a-size-medium a-color-base a-text-normal"]}, limit = 5):
24+
products.append(sp.text)
25+
for p in soup.find_all("span", {"class": "a-price-whole"}, limit = 5):
26+
prices.append(priceToInt(p.text)) #Extracting names of product and their prices
27+
28+
if products and prices:
29+
return cheapest(products, prices, name)
30+
else:
31+
print(name + " search failed.")
32+
33+
34+
def flipkart(item):
35+
URL = "https://www.flipkart.com/search?q=" + item
36+
page = requests.get(URL, headers = headers)
37+
soup = BeautifulSoup(page.content, "html.parser")
38+
name = "Flipkart"
39+
#For access to product links un-comment these:
40+
#links = []
41+
42+
products = []
43+
prices = []
44+
for a in soup.find_all("a", {"class": "s1Q9rs"}, limit = 5):
45+
products.append(a.text)
46+
#links.append(a["href"])
47+
for p in soup.find_all("div", {"class": "_30jeq3"}, limit = 5):
48+
prices.append(priceToInt(p.text)) #Extracting names of product and their prices
49+
50+
if products and prices:
51+
return cheapest(products, prices, name)
52+
else:
53+
print(name + " search failed.")
54+
55+
def snapdeal(item):
56+
URL = "https://www.snapdeal.com/search?keyword=" + item.replace(" ", "+")
57+
page = requests.get(URL, headers = headers)
58+
soup = BeautifulSoup(page.content, "html.parser")
59+
name = "Snapdeal"
60+
61+
products = []
62+
prices = []
63+
64+
for sp in soup.find_all("p", {"class": "product-title"}, limit = 5):
65+
products.append(sp.text)
66+
for p in soup.find_all("span", {"class": "lfloat product-price"}, limit = 5):
67+
prices.append(priceToInt(p.text)) #Extracting names of product and their prices
68+
69+
if products and prices:
70+
return cheapest(products, prices, name)
71+
else:
72+
print(name + " search failed.")
73+
74+
75+
#HELPER FUNCTIONS
76+
77+
def cheapest(products, prices, name):
78+
#Prints top 5 products and returns the cheapest price
79+
productList = list(zip(products, prices))
80+
productList.sort(key=lambda x: x[1])
81+
print(name.upper() + " TOP 5 PRODUCTS:")
82+
print(tabulate(productList, headers = ["Product Name", "Price (Rs.)"]), end = "\n\n")
83+
return productList[0][1] #Returns only the cheapest price for each website for final comparison
84+
85+
def priceToInt(price):
86+
#Converts the text scraped from website into integer for proper comparison
87+
converted_price = []
88+
for i in price:
89+
if i.isdigit():
90+
converted_price.append(i) #Extracting only digits
91+
92+
converted_price = int("".join(converted_price)) #Converting the string price to integer for comparison
93+
return converted_price
94+
95+
96+
if __name__ == "__main__":
97+
item = input("Enter the item you would like to search for: ")
98+
amazonPrices = [amazon(item), "Amazon"]
99+
flipkartPrices = [flipkart(item), "Flipkart"]
100+
snapdealPrices = [snapdeal(item), "Snapdeal"]
101+
if amazonPrices[0] and flipkartPrices[0] and snapdealPrices[0]:
102+
bestPrice = min(amazonPrices, flipkartPrices, snapdealPrices)
103+
print("\nBest product available for your search \"{}\" is on {} at Rs.{}".format(item, bestPrice[1], bestPrice[0]))
104+
else:
105+
print("Could not get the best price.")

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /