-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathproducts_dataframe.py
557 lines (518 loc) · 22.3 KB
/
products_dataframe.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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
from proxies_randomizer import page_content
from tkinter import Tk, Label, Entry, Button, filedialog, StringVar, OptionMenu, ttk, HORIZONTAL
from scrape_aliexpress import aliexpress
from scrape_amazon import amazon
from scrape_ebay import ebay
from write_spreadsheet import spreadsheet, close, append_spreadsheet
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import WebDriverException
from selenium.webdriver.common.by import By
from threading import Thread
from os import remove, getcwd
from sys import exit, exc_info
import time
window = Tk()
window.title("Products Dataframe")
driver = ""
class Product:
def __init__(self):
self.page_url = ""
self.url = ""
self.sku = ""
self.brand = ""
self.name = ""
self.description = ""
self.bullet_points = []
self.main_image = ""
self.other_main_images = []
self.parent = []
self.variant_images = []
self.price = []
self.shipping = []
self.variant_names = []
self.currency = "usd"
self.asin = []
self.store = 0
self.alp = 1
def append_variant(self, item, num):
if num == "bullet":
self.bullet_points.append(item)
elif num == "other_main":
self.other_main_images.append(item)
elif num == "variant_images":
self.variant_images.append(item)
elif num == "price":
self.price.append(item)
elif num == "shipping":
self.shipping.append(item)
elif num == "variant_names":
self.variant_names.append(item)
elif num == "variant_asin":
self.asin.append(item)
else:
pass
title = spreadsheet()
list_titles = [title]
extraction = 0
running = True
def extract():
global running
running = True
global extraction
global driver
driver = page_content()
global title
list_titles.append(title)
product = Product()
product.currency = clicked.get().lower()
page_max = clicked2.get()
product.brand = input_brand.get()
url = str(input_url.get())
if not url.startswith("http"):
errotxt['text'] = "Error, please enter a valid url (starting with http)"
errotxt['bg'] = "red"
elif clicked.get() == "":
errotxt['text'] = "Select a currency to convert from"
errotxt['bg'] = 'red'
else:
if "aliexpress." in url:
Thread(target=update_gui).start()
product.store = 1
errotxt['text'] = ""
errotxt['bg'] = "dark blue"
def extract_aliexpress():
global running
if "/store/" in url:
process['text'] = "Extracting urls..."
process['bg'] = "dark gray"
product.page_url = url
pages_url = []
driver.get(product.page_url)
time.sleep(3)
if "/store/group/" in url:
if "login.aliexpress" in driver.current_url:
running = False
process['text'] = ""
process['bg'] = "dark blue"
errotxt['text'] = f"Aliexpress does not allow scrape this pages"
errotxt['bg'] = 'red'
driver.quit()
return
products = driver.find_element_by_css_selector("#node-gallery").find_elements_by_class_name(
"img")
for s in products:
pages_url.append(s.find_element_by_tag_name("a").get_attribute("href"))
else:
products = driver.find_elements_by_xpath(
"/html/body/div[4]/div/div/div[3]/div[13]/div/div/div[2]/div[2]/div")
for r in range(len(products)):
t = products[r].find_elements_by_tag_name("a")[0].get_attribute("data-href")
pages_url.append(f"https://{t[2:]}")
def page_aliexpress(lk):
product.url = lk
product_object1 = aliexpress(product, driver)
process['text'] = "Converting prices..."
append_spreadsheet(product_object1, driver, title)
product.url = ""
product.sku = ""
product.name = ""
product.description = ""
product.bullet_points = []
product.main_image = ""
product.other_main_images = []
product.parent = []
product.variant_images = []
product.price = []
product.shipping = []
product.variant_names = []
for i in range(len(pages_url)):
if page_max == "all":
process['text'] = f"Extracting data... {i + 1}/{len(pages_url)}"
process['bg'] = "dark gray"
product.alp = 0
try:
page_aliexpress(pages_url[i])
except:
running = False
process['text'] = ""
process['bg'] = "dark blue"
errotxt['text'] = f"error, try again or contact the support"
errotxt['bg'] = 'red'
driver.quit()
return
else:
process['text'] = f"Extracting data... {i + 1}/{int(page_max)}"
process['bg'] = "dark gray"
try:
page_aliexpress(pages_url[i])
product.alp = 0
if int(page_max) == i + 1:
break
except:
running = False
process['text'] = ""
process['bg'] = "dark blue"
errotxt['text'] = f"error, try again or contact the support"
errotxt['bg'] = 'red'
driver.quit()
return
else:
product.url = url
process['text'] = "Extracting data..."
process['bg'] = "dark gray"
try:
product_object = aliexpress(product, driver)
process['text'] = "Converting prices..."
except:
running = False
process['text'] = ""
process['bg'] = "dark blue"
errotxt['text'] = f"error, try again or contact the support"
errotxt['bg'] = 'red'
driver.quit()
return
try:
append_spreadsheet(product_object, driver, title)
except:
running = False
process['text'] = ""
process['bg'] = "dark blue"
errotxt['text'] = f"error, try again or contact the support"
errotxt['bg'] = 'red'
driver.quit()
return
running = False
process['text'] = ""
process['bg'] = "dark blue"
errotxt['text'] = "Data extracted successfully!"
errotxt['bg'] = 'light green'
driver.quit()
try:
Thread(target=extract_aliexpress).start()
except:
running = False
process['text'] = ""
process['bg'] = "dark blue"
errotxt['text'] = f"error, try again or contact the support"
errotxt['bg'] = 'red'
driver.quit()
return
elif "ebay.c" in url:
Thread(target=update_gui).start()
product.store = 2
errotxt['text'] = ""
errotxt['bg'] = "dark blue"
def extract_ebay():
global running
driver.get(url)
if "/usr/" in url:
process['text'] = "Extracting urls..."
process['bg'] = "dark gray"
driver.find_element_by_xpath("/html/body/div[2]/div[6]/div[1]/div/a").click()
time.sleep(3)
if "/sch/" in driver.current_url:
process['text'] = "Extracting urls..."
process['bg'] = "dark gray"
product.page_url = url
pages_url = []
urls = driver.find_elements_by_xpath(
"/html/body/div[5]/div[2]/div[1]/div[1]/div/div[1]/div/div[3]/div/div[1]/div/w-root/div/div[2]/ul/li")
for i in range(len(urls)):
pages_url.append(driver.find_element_by_xpath(
f"/html/body/div[5]/div[2]/div[1]/div[1]/div/div[1]/div/div[3]/div/div[1]/div/w-root/div/div[2]/ul/li[{i + 1}]/h3/a").get_attribute(
"href"))
def page_ebay(lk):
product.url = lk
product_object2 = ebay(product, driver)
process['text'] = "Converting prices..."
append_spreadsheet(product_object2, driver, title)
product.url = ""
product.sku = ""
product.name = ""
product.description = ""
product.bullet_points = []
product.main_image = ""
product.other_main_images = []
product.parent = []
product.variant_images = []
product.price = []
product.shipping = []
product.variant_names = []
product.currency = "usd"
for i in range(len(pages_url)):
if page_max == "all":
process['text'] = f"Extracting data... {i + 1}/{len(pages_url)}"
process['bg'] = "dark gray"
try:
page_ebay(pages_url[i])
except:
running = False
process['text'] = ""
process['bg'] = "dark blue"
errotxt['text'] = f"error, try again or contact the support"
errotxt['bg'] = 'red'
driver.quit()
return
else:
process['text'] = f"Extracting data... {i + 1}/{int(page_max)}"
process['bg'] = "dark gray"
try:
page_ebay(pages_url[i])
if int(page_max) == i + 1:
break
except:
running = False
process['text'] = ""
process['bg'] = "dark blue"
errotxt['text'] = f"error, try again or contact the support"
errotxt['bg'] = 'red'
driver.quit()
return
else:
product.url = url
process['text'] = "Extracting data..."
process['bg'] = "dark gray"
try:
product_object = ebay(product, driver)
process['text'] = "Converting prices..."
except:
running = False
process['text'] = ""
process['bg'] = "dark blue"
errotxt['text'] = f"error, try again or contact the support"
errotxt['bg'] = 'red'
driver.quit()
return
try:
append_spreadsheet(product_object, driver, title)
except:
running = False
process['text'] = ""
process['bg'] = "dark blue"
errotxt['text'] = f"error, try again or contact the support"
errotxt['bg'] = 'red'
driver.quit()
return
running = False
process['text'] = ""
process['bg'] = "dark blue"
errotxt['text'] = "Data extracted successfully!"
errotxt['bg'] = 'light green'
driver.quit()
try:
Thread(target=extract_ebay).start()
except:
running = False
process['text'] = ""
process['bg'] = "dark blue"
errotxt['text'] = f"error, try again or contact the support"
errotxt['bg'] = 'red'
driver.quit()
return
elif "amazon" in url:
Thread(target=update_gui).start()
product.store = 3
errotxt['text'] = ""
errotxt['bg'] = "dark blue"
def extract_amazon():
global running
if "/stores/" in url or "/s?k" in url:
process['text'] = "Extracting urls..."
process['bg'] = "dark gray"
product.page_url = url
pages_url = []
driver.get(product.page_url)
driver.execute_script("window.scrollTo(0, 1000)")
if "/stores/" in url:
urls = WebDriverWait(driver, 10).until(EC.visibility_of_any_elements_located(
(By.XPATH, "/html/body/div[1]/div[2]/div/div[5]/div/div/div/div/div/ul/li")))
for i in range(len(urls)):
pages_url.append(driver.find_element_by_xpath(
f'/html/body/div[1]/div[2]/div/div[5]/div/div/div/div/div/ul/li[{i + 1}]/div[1]/a').get_attribute(
'href'))
elif "/s?k" in url:
urls = WebDriverWait(driver, 10).until(EC.visibility_of_any_elements_located(
(By.XPATH, "/html/body/div[1]/div[2]/div[1]/div[2]/div/span[3]/div[2]/div")))
for i in range(len(urls)):
try:
pages_url.append(driver.find_element_by_xpath(
f'/html/body/div[1]/div[2]/div[1]/div[2]/div/span[3]/div[2]/div[{i + 1}]/div/span/div/div/div[2]/h2/a').get_attribute(
'href'))
except:
pass
def page_amazon(lk):
product.url = lk
product_object3 = amazon(product, driver)
process['text'] = "Converting prices..."
append_spreadsheet(product_object3, driver, title)
product.url = ""
product.sku = ""
product.name = ""
product.description = ""
product.bullet_points = []
product.main_image = ""
product.other_main_images = []
product.parent = []
product.variant_images = []
product.price = []
product.shipping = []
product.variant_names = []
product.asin = []
for i in range(len(pages_url)):
if page_max == "all":
process['text'] = f"Extracting data... {i + 1}/{len(pages_url)}"
process['bg'] = "dark gray"
try:
page_amazon(pages_url[i])
except:
running = False
process['text'] = ""
process['bg'] = "dark blue"
errotxt['text'] = f"error, try again or contact the support"
errotxt['bg'] = 'red'
driver.quit()
return
else:
process['text'] = f"Extracting data... {i + 1}/{int(page_max)}"
process['bg'] = "dark gray"
try:
page_amazon(pages_url[i])
if int(page_max) == i + 1:
break
except:
running = False
process['text'] = ""
process['bg'] = "dark blue"
errotxt['text'] = f"error, try again or contact the support"
errotxt['bg'] = 'red'
driver.quit()
return
else:
product.url = url
process['text'] = "Extracting data..."
process['bg'] = "dark gray"
try:
product_object = amazon(product, driver)
process['text'] = "Converting prices..."
except:
running = False
process['text'] = ""
process['bg'] = "dark blue"
errotxt['text'] = f"error, try again or contact the support"
errotxt['bg'] = 'red'
driver.quit()
return
try:
append_spreadsheet(product_object, driver, title)
except:
running = False
process['text'] = ""
process['bg'] = "dark blue"
errotxt['text'] = f"error, try again or contact the support"
errotxt['bg'] = 'red'
driver.quit()
return
running = False
process['text'] = ""
process['bg'] = "dark blue"
errotxt['text'] = "Data extracted successfully!"
errotxt['bg'] = 'light green'
driver.quit()
try:
Thread(target=extract_amazon).start()
except:
running = False
process['text'] = ""
process['bg'] = "dark blue"
errotxt['text'] = f"error, try again or contact the support"
errotxt['bg'] = 'red'
driver.quit()
return
else:
errotxt['text'] = "insert url from aliexpress, ebay or amazon products only"
errotxt['bg'] = "red"
extraction += 1
def save_file():
global title
folder_selected = filedialog.askdirectory()
close(folder_selected, title)
title = spreadsheet()
list_titles.append(title)
errotxt['text'] = ""
errotxt['bg'] = "dark blue"
input_url.delete(0, "end")
input_brand.delete(0, "end")
def on_closing():
global list_titles
for t in list_titles:
try:
remove(f"{getcwd()}\{t}")
except FileNotFoundError:
pass
exit()
clicked = StringVar()
clicked.set("")
options = [
"",
"EUR",
"USD",
"GBP"
]
clicked2 = StringVar()
clicked2.set("2")
options2 = [
"2",
"4",
"6",
"8",
"all"
]
drop = OptionMenu(window, clicked, *options)
drop.place(x=395, y=120, width=70, height=30)
lbtext = Label(window, text="Paste Url: ")
lbtext.place(x=50, y=90)
lbbrand = Label(window, text="Brand: ")
lbbrand.place(x=50, y=120)
errotxt = Label(window, text="", bg="dark blue")
errotxt.place(x=160, y=190)
input_url = Entry(window)
input_brand = Entry(window)
lbprice = Label(window, text="Page Currency:")
lbprice.place(x=280, y=120)
input_url.place(x=120, y=90, width=340)
input_brand.place(x=120, y=120, width=150)
lbmaxproducts = Label(window, text="Max store urls:")
lbmaxproducts.place(x=280, y=150)
drop_max_products = OptionMenu(window, clicked2, *options2)
drop_max_products.place(x=395, y=150, width=70, height=30)
btsavefile = Button(window, text="Extract", command=extract)
btsavefile.place(x=150, y=340, width=100)
btsavefile = Button(window, text="Save File", command=save_file)
btsavefile.place(x=270, y=340, width=100)
progress = ttk.Progressbar(window, orient=HORIZONTAL, length=315, mode='determinate')
progress.place(x=100, y=250)
window.protocol("WM_DELETE_WINDOW", on_closing)
process = Label(window, text="", bg="dark blue")
process.place(x=225, y=275)
def update_gui():
global driver
progress.start(10)
while True:
global running
if running:
window.update_idletasks()
try:
_ = driver.window_handles
except WebDriverException:
running = False
process['text'] = ""
process['bg'] = "dark blue"
errotxt['text'] = f"error: the browser was closed, try again"
errotxt['bg'] = 'red'
else:
progress.stop()
break
window['bg'] = "dark blue"
window.geometry("500x400")
window.mainloop()