Skip to content

Commit

Permalink
Added Amazon Snapdeal Price Comparison Flask App
Browse files Browse the repository at this point in the history
  • Loading branch information
nishitanand committed Mar 26, 2020
0 parents commit 9a9bdbe
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 0 deletions.
33 changes: 33 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<h1>Amazon Snapdeal Price Comparison Flask App</h1>

This is a Flask App for Comparing the price of a product on Amazon and Snapdeal.

First open localhost:8000/search and then enter the name of the product which you want to search.

The program then uses BeautifulSoup library and requests and does web scrapping on the Snapdeal and Amazon website.

Then the program scrapes the websites of Snapdeal and Amazon and finds the top results from the website and stores them using Lists and Dictionary data structure of Python

The program then redirects you to localhost:8000/result and displays the results of Snapdeal and Then of Amazon in a table.

The program also uses routing to to redirect you to different web pages of the app like home page, search page and results page

To use it, first write this command in the command line

pip install -r requirements.txt

or (for Mac)

pip3 install -r requirements.txt

and then to run the file write

python app.py

or (for Mac)

python3 app.py


Then open any web browser and go to localhost:8000/search and enter name of the product that you want to search.
You will be redirected to localhost:8000/result and Snapdeal and Amazon search results will be shown to you for that product.
Binary file added __pycache__/scrapper.cpython-37.pyc
Binary file not shown.
23 changes: 23 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from flask import Flask, render_template, request, redirect
from scrapper import scrapSnapdeal

app = Flask(__name__)


@app.route('/')
def index():
return "Hello WELCOME to Nishit's Price Comparison App"

@app.route('/search')
def search():
return render_template('search.html')

@app.route('/result', methods = ['POST'])
def result():
query = request.form.get('query')

result = scrapSnapdeal(query)
return render_template('result.html', result = result)

if __name__ == "__main__":
app.run(port = 8000, debug = False)
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Flask==1.1.1
requests==2.22.0
beautifulsoup4==4.8.2
78 changes: 78 additions & 0 deletions scrapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import requests
from bs4 import BeautifulSoup

def scrapSnapdeal(query):
url="https://www.snapdeal.com/search"
params={
"keyword":query,
"sort": "phtl"
}
r=requests.get(url,params=params)
soup=BeautifulSoup(r.content)

products = soup.findAll('div', attrs = {"class": "product-tuple-listing"})
result=[]
e = {
"img": None,
"title": "Showing Results from Snapdeal",
"price": None
}
result.append(e)
for product in products:
img_tag = product.find('img')
if 'src' in img_tag.attrs:
img = img_tag.attrs['src']
else:
img = img_tag.attrs['data-src']
title = product.find('p', attrs = {"class": "product-title"}).text
price = product.find('span', attrs = {"class": "product-price"}).text


d = {
"img": img,
"title": title,
"price": price
}
result.append(d)

e = {
"img": None,
"title": "Above results were from Snapdeal, Now showing Results from Amazon",
"price": None
}
result.append(e)


url="https://www.amazon.in/s"
params={
"keyword":query,
"s": "price-desc-rank"
}
headers = {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36"
}
r=requests.get(url,params=params, headers = headers)
soup=BeautifulSoup(r.content)
products = soup.findAll('div', attrs = {'class': 's-result-item'})
for product in products:
img_tag = product.find('img')
if 'src' in img_tag.attrs:
img = img_tag.attrs['src']
try:
title = product.find('span', attrs = {"class": "a-text-normal"}).text
except:
print("This was an advertisement")
try:
price = product.find('span', attrs = {"class": "a-price-whole"}).text
except:
print("This was an advertisement")
d = {
"img": img,
"title": title,
"price": "Rs. "+price
}
result.append(d)


return result

32 changes: 32 additions & 0 deletions templates/result.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1>Search Result</h1>
<table>
<tr>
<table>
<tr>
<th>Name</th>
<th>Photo</th>
<th>Price</th>
</tr>
{% for product in result %}
<tr>
<td>{{ product['title'] }}</td>
<td>
<img src="{{ product['img'] }}" alt="">
</td>
<td>{{ product['price'] }}</td>
</tr>
{% endfor %}
</table>
</tr>
</table>>
</body>
</html>
17 changes: 17 additions & 0 deletions templates/search.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<h1>Welcome to Nishit's Price Comparison Flask App</h1>
<h2>Search you product</h2>

<form action="/result" method="POST">
<label for="">Product Name</label>
<input type="text" name="query" placeholder="Enter product name">

<button type="Submit">Search</button>
</form>
</body>
</html>

0 comments on commit 9a9bdbe

Please sign in to comment.