-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsel_flow.py
78 lines (63 loc) · 2.4 KB
/
sel_flow.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
import time
from prefect import flow
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromiumService
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.core.os_manager import ChromeType
def get_table_data(driver):
# Find the table element by its ID
table = driver.find_element(By.XPATH, '//*[@id="ContentPlaceHolder2_LData"]')
# Find all rows of the table
rows = table.find_elements(By.TAG_NAME, "tr")
table_data = []
print(len(rows))
for i, row in enumerate(rows[4:]): # Skip the first four rows
if i % 100 == 0:
print(f"Processing row {i}")
# Find all cells of the row
cells = row.find_elements(By.TAG_NAME, "td")
# Extract text from each cell and append to table_data
table_data.append([cell.text for cell in cells])
return table_data
@flow(log_prints=True)
def run_selenium():
# import chromedriver_autoinstaller
# chromedriver_autoinstaller.install()
options = webdriver.ChromeOptions()
options.add_argument("--headless")
options.add_argument("--no-sandbox")
# driver = webdriver.Chrome(
# service=ChromiumService(
# ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).install(),
# options=options,
# )
# )
# service = webdriver.ChromeService(executable_path=chromedriver_bin)
# driver = webdriver.Chrome(service=service, options=options)
driver = webdriver.Chrome(options=options)
driver.get("https://oop.ky.gov/")
# Find and interact with elements using Selenium
pc_checkbox = driver.find_element(
By.XPATH, '//*[@id="ContentPlaceHolder2_chkBoards_9"]'
)
pc_checkbox.click()
all_data = []
for letter in "AB":
print(f"Scraping for {letter}...")
last_name_box = driver.find_element(
By.XPATH, '//*[@id="ContentPlaceHolder2_TLname"]'
)
last_name_box.clear()
last_name_box.send_keys(letter)
search_button = driver.find_element(
By.XPATH, '//*[@id="ContentPlaceHolder2_BSrch"]'
)
search_button.click()
time.sleep(5)
print("---getting table data...")
t_data = get_table_data(driver)
print(f"---{len(t_data)} rows retrieved...")
all_data.extend(t_data)
return all_data