-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprice_comparison_engine.py
211 lines (165 loc) · 8.63 KB
/
price_comparison_engine.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
from tkinter import *
from bs4 import BeautifulSoup
import requests
from difflib import get_close_matches
import webbrowser
root = Tk()
class Price_compare:
def __init__(self,master):
self.var = StringVar()
self.var_ebay = StringVar()
self.var_flipkart = StringVar()
self.var_amzn = StringVar()
label = Label(master, text='Enter the product')
label.grid(row=0, column=0)
entry = Entry(master, textvariable=self.var)
entry.grid(row=0, column=1)
button_find = Button(master, text='Find', bd=4,command=self.find)
button_find.grid(row=1, column=1, sticky=W, pady=8)
def find(self):
self.product = self.var.get()
self.product_arr = self.product.split()
self.n = 1
self.key = ""
self.title_flip_var = StringVar()
self.title_amzn_var = StringVar()
self.variable_amzn = StringVar()
self.variable_flip = StringVar()
for word in self.product_arr:
if self.n == 1 :
self.key = self.key + str(word)
self.n += 1
else:
self.key = self.key + '+' + str(word)
self.window =Toplevel(root)
self.window.title('Price Comparison Engine')
label_title_flip = Label(self.window, text= 'Flipkart Title:')
label_title_flip.grid(row=0,column=0,sticky=W)
label_flipkart = Label(self.window, text='Flipkart price (Rs):')
label_flipkart.grid(row=1, column=0, sticky=W)
entry_flipkart = Entry(self.window, textvariable=self.var_flipkart)
entry_flipkart.grid(row=1, column=1,sticky=W)
label_title_amzn = Label(self.window, text='Amazon Title:')
label_title_amzn.grid(row=3, column=0, sticky=W)
label_amzn = Label(self.window, text='Amazon price (Rs):')
label_amzn.grid(row=4, column=0, sticky=W)
entry_amzn = Entry(self.window, textvariable=self.var_amzn)
entry_amzn.grid(row=4, column=1,sticky=W)
self.price_flipkart(self.key)
self.price_amzn(self.key)
try:
self.variable_amzn.set(self.matches_amzn[0])
except:
self.variable_amzn.set('Product not available')
try:
self.variable_flip.set(self.matches_flip[0])
except:
self.variable_flip.set('Product not available')
option_amzn = OptionMenu(self.window, self.variable_amzn, *self.matches_amzn)
option_amzn.grid(row=3,column=1,sticky=W)
lab_amz = Label(self.window, text='Not this? Try out suggestions by clicking on the title')
lab_amz.grid(row=3,column=2,padx=4)
option_flip = OptionMenu(self.window, self.variable_flip, *self.matches_flip)
option_flip.grid(row=0, column=1, sticky=W)
lab_flip = Label(self.window, text='Not this? Try out suggestions by clicking on the title')
lab_flip.grid(row=0,column=2,padx=4)
button_search = Button(self.window, text='Search',command=self.search,bd=4)
button_search.grid(row=2, column=2, sticky=E,padx=10, pady=4)
button_amzn_visit = Button(self.window, text='Visit Site', command=self.visit_amzn,bd=4)
button_amzn_visit.grid(row=4,column=2,sticky=W)
button_flip_visit = Button(self.window, text='Visit Site', command= self.visit_flip,bd=4)
button_flip_visit.grid(row=1, column=2, sticky=W)
def price_flipkart(self,key):
url_flip = 'https://www.flipkart.com/search?q=' + str(key) + '&marketplace=FLIPKART&otracker=start&as-show=on&as=off'
self.headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
title_arr = []
self.opt_title_flip = StringVar()
source_code = requests.get(url_flip, headers=self.headers)
plain_text = source_code.text
self.soup_flip = BeautifulSoup(plain_text, "html.parser")
for title in self.soup_flip.find_all('div', {'class': '_3wU53n'}):
title_arr.append(title.text)
user_input = self.var.get().title()
self.matches_flip = get_close_matches(user_input, title_arr, 20, 0.1)
try:
self.opt_title_flip.set(self.matches_flip[0])
except IndexError:
self.opt_title_flip.set('Product not found')
try:
for div in self.soup_flip.find_all('a', {'class': '_31qSD5'}):
for each in div.find_all('div', {'class': '_3wU53n'}):
if each.text == self.opt_title_flip.get():
self.link_flip ='https://www.flipkart.com' + div.get('href')
product_source_code = requests.get(self.link_flip, headers=self.headers)
product_plain_text = product_source_code.text
product_soup = BeautifulSoup(product_plain_text, "html.parser")
for price in product_soup.find_all('div', {'class': '_1vC4OE _3qQ9m1'}):
self.var_flipkart.set(price.text[1:] + '.00')
except UnboundLocalError:
pass
def price_amzn(self,key):
url_amzn = 'https://www.amazon.in/s/ref=nb_sb_noss_2?url=search-alias%3Daps&field-keywords=' + str(key)
# Faking the visit from a browser
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
# Getting titles of all products on that page
self.title_arr = []
self.opt_title = StringVar()
source_code = requests.get(url_amzn, headers=self.headers)
plain_text = source_code.text
self.soup = BeautifulSoup(plain_text, "html.parser")
for titles in self.soup.find_all('a', {'class': 'a-link-normal a-text-normal'}):
try:
self.title_arr.append(titles.img.get('alt'))
except AttributeError:
continue
# Getting closest match of the input from user in titles
user_input = self.var.get().title()
self.matches_amzn = get_close_matches(user_input, self.title_arr, 20, 0.01)
self.opt_title.set(self.matches_amzn[0])
product_block = self.soup.find(attrs= {'title': self.opt_title.get()})
self.product_link = product_block.get('href')
product_source_code = requests.get(self.product_link, headers=headers)
product_plain_text = product_source_code.text
product_soup = BeautifulSoup(product_plain_text, "html.parser")
try:
for price in product_soup.find(attrs={'id': 'priceblock_ourprice'}):
self.var_amzn.set(price)
self.title_amzn_var.set(self.matches_amzn[0])
except TypeError:
self.var_amzn.set('None')
self.title_amzn_var.set('product not available')
def search(self):
amzn_get = self.variable_amzn.get()
self.opt_title.set(amzn_get)
product_block = self.soup.find(attrs={'title': self.opt_title.get()})
self.product_link = product_block.get('href')
product_source_code = requests.get(self.product_link, headers=self.headers)
product_plain_text = product_source_code.text
product_soup = BeautifulSoup(product_plain_text, "html.parser")
try:
for price in product_soup.find(attrs={'id': 'priceblock_ourprice'}):
self.var_amzn.set(price)
except TypeError:
self.var_amzn.set('None')
self.title_amzn_var.set('product not available')
flip_get = self.variable_flip.get()
self.opt_title_flip.set(flip_get)
try:
for div in self.soup_flip.find_all('a', {'class': '_31qSD5'}):
for each in div.find_all('div', {'class': '_3wU53n'}):
if each.text == self.opt_title_flip.get():
self.link_flip = 'https://www.flipkart.com' + div.get('href')
product_source_code = requests.get(self.link_flip, headers=self.headers)
product_plain_text = product_source_code.text
product_soup = BeautifulSoup(product_plain_text, "html.parser")
for price in product_soup.find_all('div', {'class': '_1vC4OE _3qQ9m1'}):
self.var_flipkart.set(price.text[1:] + '.00')
except UnboundLocalError:
pass
def visit_amzn(self):
webbrowser.open(self.product_link)
def visit_flip(self):
webbrowser.open(self.link_flip)
c = Price_compare(root)
root.title('Price Comparison Engine')
root.mainloop()