-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.py
241 lines (213 loc) · 9.01 KB
/
app.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
from flask import Flask, redirect, url_for, render_template, request, flash
from flask import request
import csv
from bs4 import BeautifulSoup
from msedge.selenium_tools import Edge, EdgeOptions
import pandas as pd
import json
from bs4 import BeautifulSoup as bs
from urllib.request import urlopen as uReq
import time
from bs4 import BeautifulSoup
import requests
app = Flask(__name__)
@app.route("/", methods=["POST", "GET"])
def home():
if request.method == "POST":
test = []
names = []
flipPrices = []
prodNames = []
info,price = [],[]
url = "https://flipkart.com/search?q="
q = request.form["nm"]
q = q.replace(" ","+")
# response recieved in bytes
resp = requests.get(url+q)
# parsing response content using BeautifulSoup class, so that we can perform operations on it.
parsed_html = bs(resp.content, 'html.parser')
# data cleaning
raw_data = parsed_html.find("script", attrs={"id":"is_script"})
data = raw_data.contents[0].replace("window.__INITIAL_STATE__ = ","").replace(";","")
json_data = json.loads(data)
req_data = json_data["pageDataV4"]["page"]["data"]["10003"] #[10]["widget"]["data"]["products"][3]["productInfo"]
#req_json_data = json_data["seoMeta"]["answerBox"]["data"]["renderableComponents"][0]["value"]["data"]
data_list = []
try:
for i in range(1, len(req_data)):
d = {}
jd = req_data[i]["widget"]["data"]["products"]
# print(len(jd))
# print("i: ", i, end="\n")
for j in range(len(jd)):
jd2 = jd[j]["productInfo"]["value"]
d["title"] = jd2["titles"]["title"]
d["keySpecs"] = jd2["keySpecs"]
d["rating"] = jd2["rating"]["average"]
d["ratingCount"] = jd2["rating"]["count"]
d["price"] = jd2["pricing"]["finalPrice"]["value"]
# d["warranty"] = jd2["warrantySummary"]
d["url"] = jd2["smartUrl"]
data_list.append(d)
except:
pass
# dumping data to result.json file
# print(list(data_list))
with open("flipkart"+'.json', 'w') as fp:
json.dump(data_list, fp)
# Now let us write our data to csv file
data_file = open("flipkart"+'.csv', 'w')
# create the csv writer object
csv_writer = csv.writer(data_file)
# Counter variable used for writing
# headers to the CSV file
count = 0
for data in data_list:
if count == 0:
# Writing headers of CSV file
header = data.keys()
csv_writer.writerow(header)
count += 1
# Writing data of CSV file
csv_writer.writerow(data.values())
with open('flipkart.csv') as csv_file:
reader = csv.reader(csv_file, delimiter=',')
rows = list(reader)
i,j = 0,2
while i in range(len(rows)):
try:
name = rows[j][0]
# name = " ".join(name.split(' ')[0:2])
# print(name)
# print("name = ",name)
names.append(name)
i += 1
j += 2
except:
break
print("Best results",len(names))
# print(names,len(names))
if len(names) <= 10:
flipkart_url = "https://www.flipkart.com/search?q=" + q
print(flipkart_url)
uClient = uReq(flipkart_url)
flipkartPage = uClient.read()
uClient.close()
flipkart_html = bs(flipkartPage, "html.parser")
soup = BeautifulSoup(flipkartPage, 'html.parser')
info = soup.select("[class~=s1Q9rs]")
if info == []:
info = soup.select("[class~=IRpwTa]")
flipPrices = soup.select("[class =_30jeq3]")
prodNames = [i.get('title') for i in info]
names = prodNames
df = pd.DataFrame(list(zip(prodNames, flipPrices)),
columns =['product_name', 'Flipkart_price'])
df.to_csv('flipkart.csv')
print(df)
else:
with open('flipkart.csv') as csv_file:
reader = csv.reader(csv_file, delimiter=',')
rows = list(reader)
i,j = 0,2
while i in range(len(rows)):
try:
price = rows[j][4]
# price = price[i].text
# print("price = ",price)
flipPrices.append(price)
i += 1
j += 2
except:
break
df = pd.DataFrame(list(zip(names, flipPrices)),
columns =['Product_name', 'Flipkart_price'])
print(df)
df.to_csv('flipkart.csv')
data_file.close()
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'}
amazon=''
amazonlist = []
amazonName = []
i = 0
while i in range(len(names)):
print(names[i])
def amazon(name):
try:
global amazon
name = " ".join(name.split(' ')[0:2])
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()
amazonlist.append(amazon_price)
print("Amazon:")
print(amazon_name)
amazonName.append(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'
amazonlist.append(amazon_price)
amazonName.append("No similar product")
break
return amazon_price
except:
print("amazon: No product found!")
print("-----------------------")
amazon_price = '0'
amazonlist.append(amazon_price)
amazonName.append("No similar product")
return amazon_price
amazon_price = amazon(names[i])
amazon=''
i += 1
test = flipPrices
flip = flipPrices
idk = []
for i in range(len(flip)):
# x =
try:
x = flip[i].text.replace('₹','')
print(x)
idk.append(x)
except:
idk = test
df = pd.DataFrame(list(zip(names,idk,amazonName, amazonlist)),
columns =["Product_name_Flipkart","Flipkart_price",'Product_name_Amazon', 'Amazon_price'])
print(df)
df.to_csv('flipkartandamazon.csv')
filename = 'flipkartandamazon.csv'
data = pd.read_csv(filename, header=0)
stocklist = list(data.values)
return render_template('index.html', stocklist=stocklist)
else:
return render_template("index.html")
@app.route('/stocks')
def Stocks():
key = "YO"
filename = 'flipkartandamazon.csv'
data = pd.read_csv(filename, header=0)
stocklist = list(data.values)
return render_template('stocks.html', stocklist=stocklist, key = key)
@app.route("/<usr>")
def user(usr):
return f"<h1>{usr}</h1>"
if __name__ == "__main__":
app.run(debug=True)