-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem-2.py
More file actions
354 lines (306 loc) · 12.1 KB
/
Copy pathProblem-2.py
File metadata and controls
354 lines (306 loc) · 12.1 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
# Import libraries
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
from selenium.webdriver.common.keys import Keys
import time
import re
import pandas as pd
path = "../dataset/company_list.csv"
# Load companies from CSV
def load_companies_from_csv(file_path=path):
print(f"📂 Loading companies from {file_path}")
df = pd.read_csv(file_path)
if "company_name" not in df.columns or "url" not in df.columns:
raise ValueError("CSV must contain 'company_name' and 'url'")
return list(df[["company_name", "url"]].itertuples(index=False, name=None))
# Setup browser
def setup_driver(headless=False):
options = webdriver.ChromeOptions()
options.add_argument("--disable-notifications")
options.add_argument("--disable-popup-blocking")
options.add_argument("--disable-infobars")
options.add_experimental_option("prefs", {
"profile.default_content_setting_values.cookies": 1,
"profile.block_third_party_cookies": False
})
if headless:
options.add_argument("--headless=new")
driver = webdriver.Chrome(options=options)
driver.maximize_window()
return driver
# Accept cookies
def accept_cookies(driver):
cookie_locators = [
(By.NAME, 'cookieAgree'),
(By.CLASS_NAME, 'wscrOk'),
(By.ID, 'onetrust-accept-btn-handler'),
(By.XPATH, "//button[contains(., 'Accept')]"),
]
try:
WebDriverWait(driver, 5).until(
EC.presence_of_element_located((By.TAG_NAME, 'body'))
)
for by, selector in cookie_locators:
try:
btn = WebDriverWait(driver, 2).until(
EC.element_to_be_clickable((by, selector))
)
btn.click()
return True
except:
continue
print("❌ No cookie banner found")
except Exception as e:
pass
return False
# Handle Diageo age gate
def handle_diageo_age_verification(driver):
if "diageo.com" in driver.current_url:
try:
year_input = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "age_select_year_of_birth"))
)
year_input.send_keys("1996")
driver.find_element(By.ID, "age_confirm_btn").click()
print("✅ Diageo age gate passed")
time.sleep(3)
return True
except:
print("⚠️ Diageo age gate failed")
return False
# Click search icon (for sites like Rolls-Royce)
def click_search_icon_if_present(driver):
if "rolls-royce.com" in driver.current_url:
try:
search_icon = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.CLASS_NAME, "Nav-search__button.icon-Icon_Search"))
)
search_icon.click()
time.sleep(1)
print("✅ Search icon clicked (Rolls-Royce)")
return True
except:
print("❌ No search icon found (Rolls-Royce)")
return False
# Navigate using internal search bar
def navigate_using_site_search(driver, base_url, tech_term):
"""
Navigates to a page related to the given technology term.
Returns final URL after search
"""
print(f"\n🔍 Searching for '{tech_term}' on {base_url}")
driver.get(base_url)
time.sleep(2)
# Accept cookies
accept_cookies(driver)
# Special case: Diageo
if "diageo.com" in base_url:
handle_diageo_age_verification(driver)
# Custom strategies per website
if "rolls-royce.com" in base_url:
if click_search_icon_if_present(driver):
search_input = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.CSS_SELECTOR, ".search-query.search-txt.Nav-search__input.ui-autocomplete-input"))
)
search_input.clear()
search_input.send_keys(tech_term)
time.sleep(1)
search_input.send_keys(Keys.RETURN)
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.TAG_NAME, "body")))
time.sleep(2)
return driver.current_url
elif "baesystems.com" in base_url:
try:
search_input = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.CSS_SELECTOR, "input[type='text'][placeholder*='Search']"))
)
search_input.clear()
search_input.send_keys(tech_term)
time.sleep(1)
search_input.send_keys(Keys.RETURN)
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.TAG_NAME, "body")))
time.sleep(2)
return driver.current_url
except:
print("❌ BAE Systems search bar not found")
elif "gsk.com" in base_url:
try:
search_input = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.CLASS_NAME, "site-search__input"))
)
search_input.clear()
search_input.send_keys(tech_term)
time.sleep(1)
search_input.send_keys(Keys.RETURN)
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.TAG_NAME, "body")))
time.sleep(2)
return driver.current_url
except:
print("❌ GSK search bar not found")
elif "bt.com" in base_url:
try:
search_input = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.ID, "txtSearch"))
)
search_input.clear()
search_input.send_keys(tech_term)
time.sleep(1)
search_input.send_keys(Keys.RETURN)
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.TAG_NAME, "body")))
time.sleep(2)
return driver.current_url
except:
print("❌ BT search bar not found")
elif "vodafone.com" in base_url:
try:
search_input = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.CSS_SELECTOR, ".searchBar__input"))
)
search_input.clear()
search_input.send_keys(tech_term)
time.sleep(1)
search_input.send_keys(Keys.RETURN)
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.TAG_NAME, "body")))
time.sleep(2)
return driver.current_url
except:
print("❌ Vodafone search bar not found")
elif "jaguarlandrover.com" in base_url:
try:
search_input = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.NAME, "q"))
)
search_input.clear()
search_input.send_keys(tech_term)
time.sleep(1)
search_input.send_keys(Keys.RETURN)
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.TAG_NAME, "body")))
time.sleep(2)
return driver.current_url
except:
print("❌ Jaguar Land Rover search bar not found")
# Fallback: Try finding any visible search input
try:
search_input = None
search_locators = [
(By.TAG_NAME, "input"),
(By.CLASS_NAME, "search"),
(By.ID, "search"),
(By.NAME, "q"),
(By.NAME, "query"),
(By.XPATH, "//input[@type='text' and contains(@placeholder, 'Search')]")
]
for by, selector in search_locators:
try:
search_input = driver.find_element(by, selector)
break
except:
continue
if search_input:
search_input.clear()
search_input.send_keys(tech_term)
time.sleep(1)
search_input.send_keys(Keys.RETURN)
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.TAG_NAME, "body")))
time.sleep(2)
print(f"📌 Searched for '{tech_term}' → {driver.current_url}")
return driver.current_url
else:
print("❌ Could not find search bar — falling back to homepage")
return base_url
except Exception as e:
print(f"⚠️ Error navigating via search bar: {e}")
return base_url
# Detect technologies in current page
def detect_technologies(driver, tech_term):
print(f"🔎 Scanning for {tech_term} tools...")
html = driver.page_source
text = driver.find_element(By.TAG_NAME, "body").text
combined = html + text
cloud_tools = ["AWS", "Azure", "GCP", "Amazon Web Services", "Microsoft Azure", "Google Cloud Platform"]
mes_tools = ["Siemens Opcenter", "Rockwell FactoryTalk", "FactoryTalk", "Wonderware", "Preactor", "SIMATIC IT"]
plm_tools = ["Teamcenter", "Windchill", "ENOVIA", "Aras Innovator", "Dassault ENOVIA"]
def detect_match(tools, content):
for tool in tools:
if re.search(tool, content, re.IGNORECASE):
return tool
return "-"
if tech_term == "cloud":
return {"tool": detect_match(cloud_tools, combined), "source": driver.current_url}
elif tech_term == "MES":
return {"tool": detect_match(mes_tools, combined), "source": driver.current_url}
elif tech_term == "PLM":
return {"tool": detect_match(plm_tools, combined), "source": driver.current_url}
else:
return {"tool": "-", "source": driver.current_url}
# Scrape one company for all tech categories
def scrape_tech_website(company_name, url):
print(f"\n🌐 Scraping Tech for: {company_name}")
result = {
"Company": company_name,
"Website": url,
"Cloud Tool Mentioned": "-",
"Cloud Source URL": url,
"MES Tool Mentioned": "-",
"MES Source URL": url,
"PLM Tool Mentioned": "-",
"PLM Source URL": url
}
# --- CLOUD ---
driver = setup_driver()
driver.get(url)
accept_cookies(driver)
if "diageo.com" in url:
handle_diageo_age_verification(driver)
cloud_result = detect_technologies(driver, "cloud")
result.update({
"Cloud Tool Mentioned": cloud_result["tool"],
"Cloud Source URL": cloud_result["source"]
})
driver.quit()
# --- MES ---
driver = setup_driver()
driver.get(url)
accept_cookies(driver)
if "diageo.com" in url:
handle_diageo_age_verification(driver)
mes_result = detect_technologies(driver, "MES")
result.update({
"MES Tool Mentioned": mes_result["tool"],
"MES Source URL": mes_result["source"]
})
driver.quit()
# --- PLM ---
driver = setup_driver()
driver.get(url)
accept_cookies(driver)
if "diageo.com" in url:
handle_diageo_age_verification(driver)
plm_result = detect_technologies(driver, "PLM")
result.update({
"PLM Tool Mentioned": plm_result["tool"],
"PLM Source URL": plm_result["source"]
})
driver.quit()
return result
# Main function
def main():
companies = load_companies_from_csv()
all_results = []
for idx, (name, url) in enumerate(companies):
print(f"\n🚀 [{idx+1}/{len(companies)}] Detecting technologies for: {name}")
result = scrape_tech_website(name, url)
all_results.append(result)
print("-" * 60)
for key, value in result.items():
print(f"{key + ':':<25} {value}")
print("-" * 60)
# Save results to file
df = pd.DataFrame(all_results)
df.to_csv("technology_detection.csv", index=False, encoding="utf-8")
df.to_excel("technology_detection.xlsx", index=False)
print("\n✅ Data saved to technology_detection.csv and .xlsx")
if __name__ == "__main__":
main()