-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscraping_selenium.py
51 lines (41 loc) · 1.71 KB
/
scraping_selenium.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
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains
import time
def people_also_ask(url):
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.add_argument('log-level=1')
cService = Service(executable_path='PATH_TO_SELENIUM_WEBDRIVER')
driver = webdriver.Chrome(service=cService,options=chrome_options)
driver.get(url)
try:
wait = WebDriverWait(driver, 5)
questions = wait.until(EC.presence_of_all_elements_located((By.XPATH, "//div[@jsname='yEVEwb']")))
except Exception as e:
# print(f"People Also Asked questions dont exist for this category")
driver.close()
driver.quit()
return []
all_span_texts = []
for div in questions:
try:
div.click()
time.sleep(1)
span_elements = div.find_elements(By.TAG_NAME, 'span')
for span in span_elements:
text = span.text.strip()
if text and text not in all_span_texts:
all_span_texts.append(text)
except Exception as e:
print(f"Error clicking or extracting from div/span: {e}")
driver.close()
driver.quit()
return all_span_texts[0:2]