Price Comparison Tool
import requests
from bs4 import BeautifulSoup
app = Flask(__name__)
def scrape_amazon(product_name):
try:
url = f”https://www.amazon.in/s?k={product_name.replace(‘ ‘, ‘+’)}”
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’}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, ‘html.parser’)
products = []
for item in soup.select(‘.s-result-item’):
name = item.select_one(‘.s-title-instructions-style h2’)
price = item.select_one(‘.a-price .a-offscreen’)
if name and price:
products.append({
‘name’: name.text.strip(),
‘price’: price.text,
‘site’: ‘Amazon’
})
return products[:3] # Return top 3 results
except Exception as e:
print(f”Amazon Error: {e}”)
return []
def scrape_flipkart(product_name):
try:
url = f”https://www.flipkart.com/search?q={product_name.replace(‘ ‘, ‘%20’)}”
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’}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, ‘html.parser’)
products = []
for item in soup.select(‘div[data-id]’):
name = item.select_one(‘div._4rR01T’)
price = item.select_one(‘div._30jeq3’)
if name and price:
products.append({
‘name’: name.text.strip(),
‘price’: price.text,
‘site’: ‘Flipkart’
})
return products[:3]
except Exception as e:
print(f”Flipkart Error: {e}”)
return []
@app.route(‘/’)
def home():
return render_template(‘index.html’)
@app.route(‘/search’, methods=[‘POST’])
def search():
product_name = request.form[‘product’]
results = []
results += scrape_amazon(product_name)
results += scrape_flipkart(product_name)
# Add similar functions for Meesho and Myntra
return jsonify(results)
if __name__ == ‘__main__’:
app.run(debug=True)