-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract_ipibox.py
More file actions
80 lines (62 loc) · 2.15 KB
/
extract_ipibox.py
File metadata and controls
80 lines (62 loc) · 2.15 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
import os
import pandas as pd
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
df = pd.read_csv('all_products_with_deatails.csv')
urls = df['Links']
# Set up the webdriver
driver = webdriver.Chrome()
driver.maximize_window()
wait = WebDriverWait(driver, 10)
class_names = [
"ipiBoxi",
# "ipiBoxi_tag"
]
ipiBoxi = []
# ipiBoxi_tag = []
i = 0
for url in urls:
driver.get(url)
title = driver.title
text_data = {}
for class_name in class_names:
# elements = driver.find_elements(By.CLASS_NAME, class_name)
wait.until(lambda driver: driver.find_elements(By.CLASS_NAME, class_name))
elements = driver.find_elements(By.CLASS_NAME, class_name)
if len(elements) > 0:
text_data[class_name] = [element.text for element in elements]
# print(f"Found {len(elements)} elements with class name '{class_name}'.")
else:
text_data[class_name] = 'None'
# Save the text data to a file
if len(text_data) > 0:
with open('data.txt', 'a', encoding='utf-8') as f:
for class_name, texts in text_data.items():
if class_name == "ipiBoxi" :
f.write(str(i))
f.write('\n')
f.write( url)
# f.write('\n')
# f.write(str(class_name))
# f.write('\n')
if type(texts) == list:
my_list = []
for itemm in texts:
my_list.append(str(itemm))
# f.write('|NEXT|')
f.write('\n')
f.write(str(itemm))
# f.write('\n')
ipiBoxi.append(my_list)
else:
f.write('\n')
f.write('None')
f.write('\n')
i += 1
all_data = {
'url' : urls, 'ipiBoxi' : ipiBoxi
}
df = pd.DataFrame(all_data)
df.to_csv('Test.csv')