-
Notifications
You must be signed in to change notification settings - Fork 4
/
flipkart-scrap.py
executable file
·256 lines (218 loc) · 9.43 KB
/
flipkart-scrap.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#!/usr/bin/env python3
#^bang, enables the user to type just ./flipkart-scrap.py
#################################################################################
# import the libraries
from bs4 import BeautifulSoup as soup
import requests
import os
import matplotlib.pyplot as plt
names = []
prices = []
##################################################################################
def banner():
print(r'''
__ _ _ _ _
/ _| (_)_ __ | | ____ _ _ __| |_
| |_| | | '_ \| |/ / _` | '__| __|
| _| | | |_) | < (_| | | | |_
|_| |_|_| .__/|_|\_\__,_|_| \__| @rawho v1
|_|
''')
##################################################################################
#checking whether the file is already present
def is_file_already_present(filename):
lst = os.listdir()
return filename in lst
#####################################################################################
#creating a filename
def create_file():
filename = input('save the file as ....: ')
filename = filename + '.csv'
return filename
#################################################################################
# writing the header
def header_():
global f
f = open(filename, 'w')
header = 'NAME, PRICE, RATING, NO:OF RATING, NO:OF REVIEW \n'
f.write(header)
############################################################################
# requesting and souping the html
def requesting(url):
uClient = requests.get(url)
page_html = uClient.text
page_soup = soup(page_html, 'html.parser')
return page_soup
############################################################################
#scraping the details of the product in one model
def details(page_soup):
global f
containers = page_soup.find_all('div', {'class':'_3O0U0u'})
for container in containers:
if container is None: continue
for i in container:
#product name
name = i.div.div.img['alt']
print('PRODUCT NAME : ', name)
names.append(name)
#product price
price_container = i.find('div', {'class':'_1vC4OE'})
if price_container is None:
price = 'NA'
print('PRODUCT PRICE : ', price)
else:
price = price_container.text.strip()
print('PRODUCT PRICE : ', price)
prices.append(price)
#rating
rating_container = i.find('div', {'class':'hGSR34'})
if rating_container is None:
rating = 'NA'
print('PRODUCT RATING : ', rating)
else:
rating = rating_container.text
print('PRODUCT RATING : ', rating)
#no:of ratings
no_ratings_container = i.find('span', {'class':'_38sUEc'})
if no_ratings_container is None:
no_ratings = 'NA'
print('NO:OF RATINGS : ' + no_ratings)
else:
no_ratings1 = no_ratings_container.text
try:
no_rating = no_ratings1.split('&').split()
no_ratings = no_rating[0]
print('NO:OF RATINGS : ' + no_ratings)
except:
no_rating = no_ratings1.split()
no_ratings = no_rating[0]
print('NO:OF RATINGS : ' + no_ratings)
#no:of ratings
no_reviews_container = i.find('span', {'class':'_38sUEc'})
if no_reviews_container is None:
no_reviews = 'NA'
print('NO:OF RATINGS : ' + no_ratings)
else:
no_reviews1 = no_reviews_container.text
try:
no_review = no_reviews1.split('&').split()
no_reviews = no_rating[3] #3(+1 but counting starts from 0) becouse most products return ['68', 'Ratings', '&', '5', 'Reviews'], where five is the 4th element
print('NO:OF REVIEWS : ' + no_reviews)
except:
no_review = no_reviews1.split()
no_reviews = no_rating[3]
print('NO:OF REVIEWS : ' + no_reviews)
print('==========================')
f.write(name.replace(',','|') + ',' + price.replace(',','').replace('₹','') + ',' + rating + ',' + no_ratings.replace(',','').replace('(','').replace(')','') + ',' + no_reviews.replace(',','').replace('(','').replace(')','') + '\n')
###########################################################################################################
#scraping the details of product in another way
def details2(page_soup):
global f
containers = page_soup.find_all('div', {'class':'bhgxx2 col-12-12'})
for container in containers:
try:
name_container = container.find('div', {'class':'_3wU53n'})
if name_container is None : continue
name = name_container.text.strip()
print('PRODUCT NAME : ' + name)
names.append(name)
price_container = container.find('div',{'class':'_1vC4OE _2rQ-NK'})
if price_container is None:
price = 'NA'
print('PRODUCT PRICE : ' + price)
else:
price1 = price_container.text.strip()
price = price1.replace(',','')
print('PRODUCT PRICE : ' + price)
prices.append(price)
rating_container = container.find('div', {'class':'hGSR34'})
if rating_container is None:
rating = 'NA'
print('PRODUCT PRICE : ' + price)
else:
rating = rating_container.text.strip()
print('PRODUCT RATING : ' + rating)
no_ratings_container = container.find('span', {'class': '_38sUEc'}).span.span
if no_ratings_container is None:
no_ratings = 'NA'
no_reviews = 'NA'
print('NO:OF RATINGS : ' + no_ratings)
else:
no_ratings1 = no_ratings_container.text.strip()
try:
no_rating = no_ratings1.split('&').split()
no_ratings = no_rating[0]
no_reviews = no_rating[3] #3(+1 but counting starts from 0) becouse most products return ['68', 'Ratings', '&', '5', 'Reviews'], where five is the 4th element
print('NO:OF RATINGS : ' + no_ratings)
print('NO:OF REVIEWS : ' + no_ratings)
except:
no_rating = no_ratings1.split()
no_ratings = no_rating[0]
no_reviews = no_rating[3]
print('NO:OF RATINGS : ' + no_ratings)
print('NO:OF REVIEWS : ' + no_ratings)
print('==========================================================')
f.write(name.replace(',','|') + ',' + price.replace(',','').replace('₹','') + ',' + rating + ',' + no_ratings.replace(',','').replace('(','').replace(')','') + no_reviews.replace(',','').replace('(','').replace(')','') + '\n')
except:
continue
#########################################################################################
def oh_yeah():
my_url = f'https://www.flipkart.com/search?q={my_product}&otracker=search&otracker1=search&marketplace=FLIPKART&as-show=on&as=off'
page_soup = requesting(my_url)
#takings the links of all the page
page_container = page_soup.find_all('a','_2Xp0TH')
#looping through the each page
for page in page_container:
url1 = 'https://www.flipkart.com'
url2 = page.get('href')
url = url1 + url2
soup1 = requesting(url)
try:
details(soup1)
except:
details2(soup1)
print(names)
print(prices)
#########################################################################################
def plot(filename):
filename = filename.split('.', 1)[0]
#converting prices from string to int for sorting
newprices = [i[1:] for i in prices]
newprices1 = [i.replace(',','') for i in newprices]
intprices = [int(i) for i in newprices1]
#sorting according to prices
sort_prices, sort_names = zip(*sorted(zip(intprices, names)))
#plotting
plt.barh(sort_names, sort_prices)
#to show price value for each bar
for i, v in enumerate(sort_prices):
plt.text(v, i, " "+str(v), color='blue', va='center', fontweight='bold')
plt.yticks(fontsize = 6, wrap = True)
plt.xticks(rotation=90)
plt.savefig(filename + '.png')
#plt.show()
########################## MAIN ###########################################
banner()
my_product = input('Enter the flipcart product : ')
filename = create_file()
while True:
if is_file_already_present(filename):
aa = input('filename already exits. Do u want to replace it (y/n) : ')
if aa == 'n':
filename = create_file()
if is_file_already_present(filename): continue
header_()
oh_yeah()
plot(filename)
break
else:
header_()
oh_yeah()
plot(filename)
break
else:
header_()
oh_yeah()
plot(filename)
break
################################################################################