-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelper_scraping.py
More file actions
367 lines (278 loc) · 8.89 KB
/
helper_scraping.py
File metadata and controls
367 lines (278 loc) · 8.89 KB
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
from bs4 import BeautifulSoup
import warnings
warnings.filterwarnings("ignore")
import pandas as pd
import time
import requests
'''
this function gives us the number of pages we have to itterate for each category
'''
def page_finder(url):
website = requests.get(url)
soup = BeautifulSoup(website.content, 'html.parser')
finder = soup.find_all(class_={"pagination-title__option-item"})
pages = finder[0].get_text()
number_of_pages = pages[12:15]
return number_of_pages
'''
this function gives us the url of pages we have to itterate for each category
'''
def category_urls(url):
num_pages = int(page_finder(url))
liste = []
for i in range(1, num_pages, 1):
liste.append(url + "?page=" + str(i))
return liste
'''
this function gives us the rating and the id of every product
'''
def id_rating_scraper(url):
http = httplib2.Http()
website = requests.get(url)
soup = BeautifulSoup(website.content, 'html.parser')
list_new = []
time.sleep(5)
for link in soup.find_all('a',class_ = 'link link--no-decoration product-tile__main-link', href=True):
list_new.append(link['href'])
star_finder = soup.find_all('div', class_={'product-tile__details'})
star_list = []
time.sleep(random.randint(5,16))
for i in star_finder:
x = i.find('span', class_={'product-tile__ratings-info'})
if x == None:
star_list.append(None)
else:
star_list.append(x.get_text())
return list_new, star_list
'''
this function return the rating cleaned
'''
def id_rating_cleaner(url):
x,y = id_rating_scraper(url)
time.sleep(10)
url = []
for i in x:
url.append("https://www.website.de"+ i)
new_y = []
for i in y:
if i != None:
new_y.append(i.replace('\xa0',' '))
else:
new_y.append(None)
rating, num_rating = [],[]
for i in new_y:
if i != None:
rating.append(i.split('(')[0].strip())
num_rating.append(i.split('(')[1][:-1])
else:
rating.append(None)
num_rating.append(None)
df_url_ratin = pd.DataFrame(
{"url": url,
"rating": rating,
"number_rating": num_rating,
})
return df_url_rating
"""
This functions gets the size of the product.
The size is given in ml (50 ml) or in counting (1 Stk, 1 Stück)
"""
def size_getter(url):
website = requests.get(url)
soup = BeautifulSoup(website.content, 'html.parser')
try:
ml_finder = soup.find_all(class_={'product-detail__variant-name'})
ml_finder
ml = []
for i in ml_finder:
ml.append(i.get_text())
size_one = ml[0]
return size_one
except:
ml = []
ml.append("None")
return size_one
"""
This functions gets the original price of the product in Euro (24,99€).
If there is a discounted price the original price is strikethrough and the function will take the strikethrough price.
"""
def price_getter(url):
website = requests.get(url)
soup = BeautifulSoup(website.content, 'html.parser')
price_finder = soup.find_all(class_={'product-price'})
price_finder
prices_2 = []
for i in price_finder:
prices_2.append(i.get_text())
price_two = prices_2[0].replace('\xa0', '')
return price_two
"""
This functions gets the brand of the product (Yves Saint Laurent).
"""
def brand_getter(url):
website = requests.get(url)
soup = BeautifulSoup(website.content, 'html.parser')
brand_finder = soup.find_all(class_={'brand-logo__text brand-logo__text--dynamic'})
brand_finder
for i in brand_finder:
brand_3 = i.get_text()
brand = brand_3
return brand
"""
This functions gets the typ of the product (Parfum, Set, Mascara).
"""
def typ_getter(url):
website = requests.get(url)
soup = BeautifulSoup(website.content, 'html.parser')
typ_finder = soup.find_all(class_={'third-line'})
typ_finder
for i in typ_finder:
typ = i.get_text()
return typ
"""
This functions gets the name of the product (Good GirlEau de Parfum Spray).
"""
def product_getter(url):
website = requests.get(url)
soup = BeautifulSoup(website.content, 'html.parser')
product_finder = soup.find_all(class_={'second-line'})
product_finder
for i in product_finder:
products_list = i.get_text()
return products_list
"""
This functions gets more details of the product
ITEM_NB = The item number (031546)
EFFECT = The effect of the product (refining / verfeinernd)
AGE = The age for which the product is recommended
CHARESTERISTICS = characteristics of the product (smoothing, protective / glättend, schützend)
PRODUCT AWARD = What distinguishes the product in particular (microplastic-free, paraffin-free)
SCOPE = Is the area of application for which the product is suitable (face / Gesicht)
"""
def details_getter(url):
website = requests.get(url)
soup = BeautifulSoup(website.content, 'html.parser')
details = []
details_finder = soup.find_all(class_={'classification__item'})
for i in details_finder:
details.append(i.get_text())
details_iter = iter(details)
details_dict_object = itertools.zip_longest(details_iter, details_iter, fillvalue=None)
details_dict = dict(details_dict_object)
for i in details_dict:
try:
item_nb = details_dict['Art-Nr.']
except:
item_nb = "0"
try:
effect = details_dict['Effekt']
except:
effect = "None"
try:
age = details_dict['Alter']
except:
age = "None"
try:
charesteristics = details_dict['Eigenschaft']
except:
charesteristics = "None"
try:
product_award = details_dict['Produktauszeichnung']
except:
product_award = "None"
try:
scope = details_dict['Anwendungsbereich']
except:
scope = "None"
detail_list = [item_nb, effect, age, charesteristics, product_award, scope]
return detail_list
"""
This functions gets the name of the product (FACE / GESICHT, BODY / KÖRPER ).
"""
def category_getter(url):
website = requests.get(url)
soup = BeautifulSoup(website.content, 'html.parser')
try:
category_finder = soup.find_all(class_={'link link--text'})
time.sleep(1)
category = category_finder[0].get_text()
return category
except:
category = "None"
return category
"""
This functions calls all the functions to get the product details and returns the data in a dataframe
"""
def get_data(url_list):
sizes_list = []
prices_list = []
brand_list = []
product_list = []
typ_list = []
item_nb_list = []
effect_list = []
age_list = []
charesteristics_list = []
product_award_list = []
scope_list = []
category_list = []
for i in url_list:
size = size_getter(i)
sizes_list.append(size)
time.sleep(random.randint(1,4))
price = price_getter(i)
prices_list.append(price)
brand_1 = brand_getter(i)
brand_list.append(brand_1)
time.sleep(random.randint(1,4))
typ = typ_getter(i)
typ_list.append(typ)
product = product_getter(i)
product_list.append(product)
time.sleep(random.randint(1,4))
details = details_getter(i)
try:
item_nb_list.append(details[0])
except:
item_nb_list.append('Null')
try:
effect_list.append(details[1])
except:
effect_list.append('Null')
try:
age_list.append(details[2])
except:
age_list.append("Null")
try:
charesteristics_list.append(details[3])
except:
charesteristics_list.append("Null")
try:
product_award_list.append(details[4])
except:
product_award_list.append("Null")
try:
scope_list.append(details[5])
except:
scope_list.append("Null")
category = category_getter(i)
category_list.append(category)
df_02 = pd.DataFrame(
{
"item_nb": item_nb_list,
"brand": brand_list,
"product": product_list,
"size": sizes_list,
"price": prices_list,
"category": category_list,
"type": typ_list,
"effect": effect_list,
"scope": scope_list,
"age": age_list,
"charesteristics": charesteristics_list,
"product_award": product_award_list,
"url": url_list
}
)
df_02
return df_02