0% found this document useful (0 votes)
55 views9 pages

Rate Analogy

This Python program allows a user to search for and compare prices of a product across multiple websites. It defines functions to search Flipkart, Amazon, and OLX for the given product, and extract the name, price and URL. It then converts all prices to integers, stores them in a list and finds the minimum price. Finally, it prints the lowest price found along with the URL of the website offering that price.

Uploaded by

Subavani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views9 pages

Rate Analogy

This Python program allows a user to search for and compare prices of a product across multiple websites. It defines functions to search Flipkart, Amazon, and OLX for the given product, and extract the name, price and URL. It then converts all prices to integers, stores them in a list and finds the minimum price. Finally, it prints the lowest price found along with the URL of the website offering that price.

Uploaded by

Subavani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Main Program

from bs4 import BeautifulSoup


import requests
import time
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110
Safari/537.3'}

flipkart=''
amazon=''
olx=''

def flipkart(name):
try:
global flipkart
name1 = name.replace(" ","+") #iphone x -> iphone+x
flipkart=f'https://www.flipkart.com/search?
q={name1}&otracker=search&otracker1=search&marketplace=FLIPK
ART&as-show=off&as=off'
res = requests.get(f'https://www.flipkart.com/search?
q={name1}&otracker=search&otracker1=search&marketplace=FLIPK
ART&as-show=off&as=off',headers=headers)
print("\nSearching in flipkart....")
soup = BeautifulSoup(res.text,'html.parser')
flipkart_name = soup.select('._4rR01T')[0].getText().strip() ###
New Class For Product Name
flipkart_name = flipkart_name.upper()
if name.upper() in flipkart_name:
flipkart_price = soup.select('._1_WHN1')[0].getText().strip()
### New Class For Product Price
flipkart_name = soup.select('._4rR01T')[0].getText().strip()
print("Flipkart:")
print(flipkart_name)
print(flipkart_price)
print("-----------------------")
else:
print("Flipkart:No product found!")
print("-----------------------")
flipkart_price='0'
return flipkart_price
except:
print("Flipkart:No product found!")
print("-----------------------")
flipkart_price= '0'
return flipkart_price
def amazon(name):
try:
global amazon
name1 = name.replace(" ","-")
name2 = name.replace(" ","+")
amazon=f'https://www.amazon.in/{name1}/s?k={name2}'
res = requests.get(f'https://www.amazon.in/{name1}/s?
k={name2}',headers=headers)
print("\nSearching in amazon:")
soup = BeautifulSoup(res.text,'html.parser')
amazon_page = soup.select('.a-color-base.a-text-normal')
amazon_page_length = int(len(amazon_page))
for i in range(0,amazon_page_length):
name = name.upper()
amazon_name = soup.select('.a-color-base.a-text-normal')
[i].getText().strip().upper()
if name in amazon_name[0:20]:
amazon_name = soup.select('.a-color-base.a-text-normal')
[i].getText().strip().upper()
amazon_price = soup.select('.a-price-whole')
[i].getText().strip().upper()
print("Amazon:")
print(amazon_name)
print("₹"+amazon_price)
print("-----------------------")
break
else:
i+=1
i=int(i)
if i==amazon_page_length:
print("amazon : No product found!")
print("-----------------------")
amazon_price = '0'
break
return amazon_price
except:
print("amazon: No product found!")
print("-----------------------")
amazon_price = '0'
return amazon_price

def olx(name):
try:
global olx
name1 = name.replace(" ","-")
olx=f'https://www.olx.in/items/q-{name1}?isSearchCall=true'
res = requests.get(f'https://www.olx.in/items/q-{name1}?
isSearchCall=true',headers=headers)
print("\nSearching in OLX......")
soup = BeautifulSoup(res.text,'html.parser')
olx_name = soup.select('._2tW1I')
olx_page_length = len(olx_name)
for i in range(0,olx_page_length):
olx_name = soup.select('._2tW1I')[i].getText().strip()
name = name.upper()
olx_name = olx_name.upper()
if name in olx_name:
olx_price = soup.select('._89yzn')[i].getText().strip()
olx_name = soup.select('._2tW1I')[i].getText().strip()
olx_loc = soup.select('.tjgMj')[i].getText().strip()
try:
label = soup.select('._2Vp0i span')[i].getText().strip()
except:
label = "OLD"

print("Olx:")
print(label)
print(olx_name)
print(olx_price)
print(olx_loc)
print("-----------------------")
break
else:
i+=1
i=int(i)
if i==olx_page_length:
print("Olx: No product Found!")
print("-----------------------")
olx_price = '0'
break
return olx_price
except:
print("Olx: No product found!")
print("-----------------------")
olx_price = '0'
return olx_price

def convert(a):
b=a.replace(" ",'')
c=b.replace("INR",'')
d=c.replace(",",'')
f=d.replace("₹",'')
g=int(float(f))
return g

###
name=input("product name:\n")
flipkart_price=flipkart(name)
amazon_price=amazon(name)
olx_price = olx(name)
print("----------------------------------")

if flipkart_price=='0':
print("No product found!")
flipkart_price = int(flipkart_price)
else:
print("\nFLipkart Price:",flipkart_price)
flipkart_price=convert(flipkart_price)
if amazon_price=='0':
print("No Product found!")
amazon_price = int(amazon_price)
else:
print("\namazon price: ₹",amazon_price)
amazon_price=convert(amazon_price)

if olx_price =='0':
print("No product found!")
olx_price = int(olx_price)
else:
print("\nOlx Price:",olx_price)
olx_price=convert(olx_price)

time.sleep(2)

#print(f"{type(flipkart_price)} , {type(amazon_price)} ,
{type(olx_price)} ")

lst = [flipkart_price,amazon_price,olx_price]
#print(lst)
lst2=[]
for j in range(0,len(lst)):
if lst[j]>0:
lst2.append(lst[j])
if len(lst2)==0:
print("No relative product find in all websites....")
else:
min_price=min(lst2)

print("_______________________________")
print("\nMinimun Price: ₹",min_price)
price = {
f'{amazon_price}':f'{amazon}',
f'{olx_price}':f"{olx}",
f'{flipkart_price}':f'{flipkart}',
}
for key, value in price.items():
if int(key)==min_price:
print ('\nurl:', price[key])
print("\nUrls:\n")
print("-----------------------------------------------------------------------")
print(amazon)
print(olx)
print(flipkart)
print("------------------------------------------------------------------------")

You might also like