-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract_details.py
More file actions
78 lines (64 loc) · 2.13 KB
/
extract_details.py
File metadata and controls
78 lines (64 loc) · 2.13 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
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 = [
"thisbrand",
"ipih1",
"sizedetails",
"sizedetail",
"ipiBoxi"
]
brands= []
iphih1 = []
sizedetails = []
sizedetail = []
ipiBoxi = []
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)
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('text_data.txt', 'w', encoding='utf-8') as f:
for class_name, texts in text_data.items():
if class_name == "thisbrand" :
brands.append(texts)
elif class_name == "ipih1" :
iphih1.append(texts)
elif class_name == "sizedetails" :
sizedetails.append(texts)
elif class_name == "sizedetail" :
sizedetail.append(texts)
elif class_name == "ipiBoxi" :
my_list = []
for itemm in texts:
my_list.append(str(itemm))
ipiBoxi.append(my_list)
# ipiBoxi.append(texts)
else:
print("No text data found on the page.")
print(f"Page {i}_{title} DONE-------------------------------.")
i += 1
all_data = {
'url' : urls, 'brands' : brands, 'iphih1' : iphih1, 'sizedetails' : sizedetails,
'sizedetail' : sizedetail, 'ipiBoxi' : ipiBoxi
}
df = pd.DataFrame(all_data)
df.to_csv('files2test.csv')