-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsing.py
448 lines (365 loc) · 17.8 KB
/
parsing.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
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
import csv
import requests
import ast
import re
import time
from bs4 import BeautifulSoup
def get_html(url):
r = requests.get(url)
return r.text
def get_total_pages(html):
soup = BeautifulSoup(html, 'lxml')
divs = soup.find('div', class_='search-pagination-list-container')
try:
pages = divs.find_all('a')[-2].get('href')
total_pages = pages.split('=')[3]
except BaseException as e:
print(e)
total_pages = 1
return int(total_pages)
def get_product(html):
soup = BeautifulSoup(html, 'lxml')
try:
divs = soup.find('div', class_='search-main')
pages_1 = divs.find_all('a', class_='search-result-product-url') # For product_url and product_name
pages_2 = divs.find_all('div', class_='product-codes') # For product_codes
product_codes = []
products = []
for page in pages_2:
spans = page.find_all('span')
product_codes.append([spans[0].text.split(':')[1].strip(), spans[1].text.split(':')[1].strip()])
for page, codes in zip(pages_1, product_codes):
product = {}
product.update(dict({"name": page.text, "MFG": codes[0], "CDW": codes[1]}))
products.append(product)
return products
except BaseException as e:
print(e)
# If category contains only one product the site makes redirect to this product-page. Parsing product-page
try:
codes = []
base_url = "https://www.cdw.ca"
div = soup.find_all('script', type='text/javascript')[-3].text.strip()
new_url = div.split('"')[1]
new_html = get_html(base_url + new_url)
soup = BeautifulSoup(new_html, 'lxml')
divs = soup.find('div', class_='productRight')
codes.append(divs.find('span', class_='mpn').text.strip())
codes.append(divs.find('span', class_='edc').text.strip())
product = {}
product.update(dict({"name": divs.find('h1', id='primaryProductName').text.strip(), "MFG": codes[0], "CDW": codes[1]}))
return [product]
except BaseException as e:
print(e)
def get_product_categories(html):
soup = BeautifulSoup(html, 'lxml')
divs = soup.find('table')
pages = divs.find_all('div', class_='viewbulletedHyperlink SecondLevelInner')
categories = {}
for page in pages:
url = page.find('a').get('href')
if '?' in url and '&' in url:
url = url.split('?')[1]
else:
continue
category_name = page.text.strip()
if 'Software' not in category_name and 'Delivered Services' not in category_name and 'CDW BTOs' not in category_name:
categories.update(dict({category_name: url}))
return categories
def get_main_category(string):
computer_accessories = ['Computer Components', 'Computer Security Locks', 'Computer Speakers',
'Joysticks & Game Controllers', 'Keyboards & Keypads', 'Mice & Trackballs',
'Notebook Accessories', 'Notebook Carrying Cases & Accessories',
'Notebook Docks & Port Replicators', 'Signature Pads & Accessories', 'Tablet Accessories',
'Tools', 'Video Cards & Imaging', 'Webcams']
computers = ['Desktop Computers', 'Notebook Computers', 'Point of Sale Computers', 'Tablets & Tablet PCs',
'Thin Clients', 'Workstations']
data_storage_products = ['CD, DVD & Blu-Ray Drives', 'Disc Duplicators', 'Drive Arrays', 'Flash Memory',
'Floppy Disk Drives', 'Hard Drives', 'Interfaces / Controllers', 'Media & Accessories',
'Networked Attached Storage (NAS)', 'Storage Area Networking (SAN)',
'Tape Automation & Drives']
electronics = ['2-Way Radios & Accessories', 'Audio/Stereo Equipment', 'Automation & Control Systems',
'Cell/Smart Phones & Accessories', 'Digital Cameras & Accessories', 'Drones & Accessories',
'GPS Devices & Accessories', 'Handheld Devices & Accessories', 'Headphones/Earphones & Accessories',
'Microphones & Accessories', 'MP3 Players & Accessories', 'Photographic Accessories',
'Speaker Systems & Accessories', 'Televisions & Video Equipment', 'Video Camcorders & Accessories',
'Virtual Reality & Accessories']
memories = ['Cache Memory', 'Network Device Memory', 'Printer Memory', 'Server Memory', 'System Memory (RAM)']
monitors_projectors = ['CRT Monitors', 'Interactive Whiteboards & Accessories', 'Large-Format Displays',
'LCD / LED Monitors', 'Medical Displays & Accessories', 'Monitor, Display & TV Accessories',
'Projector Accessories', 'Projectors', 'Touchscreen Displays & Accessories']
networking_products = ['Communication Boards', 'Ethernet Switches', 'Modems', 'Network Interface Adapters (NIC)',
'Network Optimization Appliances', 'Network Security', 'Network Test Equipment',
'PBX/Multi-User Telephony Systems', 'Routers', 'Wireless Networking']
office_equipment = ['Books', 'Calculators & Accessories', 'Cleaning Supplies', 'Furniture', 'Mounts & Carts',
'Office Supplies', 'Paper Shredders & Accessories', 'Toys', 'Typewriters/Word Processors']
phones_video_conferencing = ['Headsets', 'Phone System Architecture', 'Phones', 'Video Conferencing']
power_cooling_racks = ['Batteries', 'Power Adapters', 'Power Inverters', 'Rack Mounting Equipment',
'Surge Suppressors', 'UPS/Battery Backup Products']
printers_scanners = ['3D Printers & Accessories', 'Barcode & Handheld Scanners', 'Business Inkjet Printers',
'CD/DVD Media Printers', 'Copy Machines & Accessories', 'Document Scanners',
'Dot-Matrix Printers', 'FAX Machines', 'Ink, Toner & Print Supplies',
'Inkjet & Photo Printers', 'Laser Printers', 'Multifunction Printers', 'Print Servers',
'Printer & Scanner Accessories', 'Printer Hard Drives', 'Printer Paper & Media',
'Thermal Printers', 'Wide-Format Printers/Plotters']
servers_server = ['KVM Switches, Consoles & Accessories', 'Server Accessories & IO Accelerators', 'Servers']
if 'Cable' in string:
return 'Cables'
elif string in computer_accessories:
return 'Computer Accessories'
elif string in computers:
return 'Computers'
elif string in data_storage_products:
return 'Data Storage Products'
elif string in electronics:
return 'Electronics'
elif string in memories:
return 'Memory'
elif string in monitors_projectors:
return 'Monitors & Projectors'
elif string in networking_products:
return 'Networking Products'
elif string in office_equipment:
return 'Office Equipment & Supplies'
elif string in phones_video_conferencing:
return 'Phones & Video Conferencing'
elif string in power_cooling_racks:
return 'Power & Cooling & Racks'
elif string in printers_scanners:
return 'Printers & Scanners & Print Supplies'
elif string in servers_server:
return 'Servers & Server Management'
else:
return 'Computer Accessories'
def write_csv(data, category_name, mod):
if mod == 'add':
use_mod = 'a'
else:
use_mod = 'w'
with open('/parsing/all_data_general.csv', use_mod, newline='') as f:
writer = csv.writer(f, delimiter=';')
writer.writerow((data['name'],
data['reference#'],
data['supplier_reference#'],
data['cost_price'],
data['price_tax_excluded'],
data['label_in_stock'],
data['quantity'],
data['img_url'],
data['features'],
data['active'],
data['description'],
data['available_for_order'],
data['category']))
def get_json(url):
try:
html = get_html(url)
soup = BeautifulSoup(html, 'lxml')
soup = soup.text
soup = soup.replace("null", "None")
specifications = ast.literal_eval(soup)
except BaseException as e:
print(e)
html = get_html(url)
soup = BeautifulSoup(html, 'lxml')
soup.replace('<html><body><p>', '')
soup.replace('</p></body></html>', '')
soup.replace('<br/>', '')
specifications = ast.literal_eval(soup)
return specifications
def get_page_data(html, products_id, products, category_name):
try:
soup = BeautifulSoup(html, 'lxml')
divs = soup.find('div', class_='search-results')
products_pages = divs.find_all('div', class_='search-result coupon-check')
for page, id, product in zip(products_pages, products_id, products):
try:
div = page.find('div', class_='search-result-price')
if div.find('div', class_='price-msrp single'):
price = ''
else:
price = div.find('div', class_='price-type-price').text.strip()
except:
price = ''
try:
div = page.find('div', class_='is-available').find_all('span')[1].text.split()
availability = ''
for i in range(1, len(div)):
availability += div[i] + ' '
availability.strip()
except:
availability = ''
try:
div = page.find('div', class_='column-1')
img_url = div.find('img').get('src')
except:
img_url = ''
try:
specifications_url = 'https://www.cdw.ca/api/product/1/data/technicalspecifications/' + id
specifications = get_json(specifications_url)
all_specifications = ""
for param in specifications["AttributeGroups"]:
attributes = param["Attributes"]
for attribute in attributes:
all_specifications += '{0}:{1}::{2}, '.format(attribute["Key"].replace('#', '№'),
re.sub("[=<>]", "", attribute["Value"].replace('#', '№')),
param["FolderName"])
except:
all_specifications = ''
try:
description_url = 'https://www.cdw.ca/api/product/1/data/overview/' + id
description = get_json(description_url)
description = description["MarketText"]
description = description.replace('<br />', '').replace(';', ',')
except:
description = ''
if 'In Stock' in availability:
active = 1
quantity = 100
available_for_order = 1
else:
active = 0
quantity = 0
available_for_order = 0
price = re.sub("[^0-9.]", "", price)
try:
if price == '':
pass
else:
data = {'name': re.sub("[=<>]", "", product.get("name").replace(';', ',').replace('#', '№')),
'category': 'Home/'+get_main_category(category_name)+'/'+(category_name.replace('/', '-'))+', '
+ get_main_category(category_name)+'/'+(category_name.replace('/', '-')) + ', Home',
'reference#': product.get("MFG").replace('=', ''),
'supplier_reference#': product.get("CDW"),
'label_in_stock': 'In Stock',
'quantity': quantity,
'cost_price': price,
'price_tax_excluded': str(round(float(price)*1.12, 2)),
'img_url': img_url.replace("CDW//", "CDW/"),
'features': all_specifications,
'active': active,
'description': description,
'available_for_order': available_for_order
}
write_csv(data, category_name, mod='add')
except:
pass
except AttributeError: # If category contains only one product the site makes redirect to this product-page. Parsing product-page
base_url = "https://www.cdw.ca/product/any/"
new_url = base_url + products_id[0] + '?enkwrd='
new_html = get_html(new_url)
soup = BeautifulSoup(new_html, 'lxml')
product = soup.find('div', class_='productMain')
try:
div = product.find('div', class_='price-type-single').find('span', class_='price-type-selected')
if div is None:
price = ''
else:
price = div.text.strip()
except:
price = ''
try:
span = product.find('span', class_='short-message-block').text.strip()
if span is not None:
availability = span
else:
availability = ''
except:
availability = ''
try:
div = product.find('div', class_='main-image')
img_url = div.find('img').get('src')
except:
img_url = ''
try:
specifications_url = 'https://www.cdw.ca/api/product/1/data/technicalspecifications/' + products_id[0]
specifications = get_json(specifications_url)
all_specifications = ""
for param in specifications["AttributeGroups"]:
attributes = param["Attributes"]
for attribute in attributes:
all_specifications += '{0}:{1}::{2}, '.format(attribute["Key"].replace('#', '№'),
re.sub("[=<>]", "", attribute["Value"].replace('#', '№')),
param["FolderName"])
except:
all_specifications = ''
try:
description_url = 'https://www.cdw.ca/api/product/1/data/overview/' + products_id[0]
description = get_json(description_url)
description = description["MarketText"]
description = description.replace('<br />', '').replace(';', ',')
except:
description = ''
if 'In Stock' in availability:
active = 1
quantity = 100
available_for_order = 1
else:
active = 0
quantity = 0
available_for_order = 0
price = re.sub("[^0-9.]", "", price)
if price == '':
pass
else:
data = {'name': re.sub("[=<>]", "", product.get("name").replace(';', ',').replace('#', '№')),
'category': 'Home/'+get_main_category(category_name)+'/'+(category_name.replace('/', '-'))+', '
+ get_main_category(category_name)+'/'+(category_name.replace('/', '-')) + ', Home',
'reference#': product.get("MFG").replace('=', ''),
'supplier_reference#': product.get("CDW"),
'label_in_stock': 'In Stock',
'quantity': quantity,
'cost_price': price,
'price_tax_excluded': str(round(float(price) * 1.12, 2)),
'img_url': img_url.replace("CDW//", "CDW/"),
'features': all_specifications,
'active': active,
'description': description,
'available_for_order': available_for_order
}
write_csv(data, category_name, mod='add')
def main():
url = "https://www.cdw.ca/search/?"
start_url = "https://www.cdw.ca/shop/search/browse.aspx"
page_part = "&pCurrent="
html = get_html(start_url)
categories = get_product_categories(html)
data = {'name': 'name',
'category': 'category',
'reference#': 'reference#',
'supplier_reference#': 'supplier_reference#',
'label_in_stock': 'label_in_stock',
'quantity': 'quantity',
'cost_price': 'cost_price',
'price_tax_excluded': 'price_tax_excluded',
'img_url': 'img_url',
'features': 'features',
'active': 'active',
'description': 'description',
'available_for_order': 'available_for_order'
}
write_csv(data, 'all_data', mod='w')
for category in categories.items(): # 135 categories
try:
category_url = category[1]
url_gen = url + category_url
total_pages = get_total_pages(get_html(url_gen))
for i in range(1, total_pages + 1):
print(category[0], ': ', i, '/', total_pages)
time.sleep(0.2)
url_gen = url + category_url + page_part + str(i)
html = get_html(url_gen)
products = get_product(html)
# Search for products codes
products_id = []
for product in products:
element = product.get("CDW")
products_id.append(element)
get_page_data(html, products_id, products, category[0])
except BaseException as e:
print(e)
continue
if __name__ == '__main__':
start_time = time.time()
main()
print("--- %s seconds ---" % (time.time() - start_time))