ALAMEDA

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import Select
import time
import csv

— CONFIGURATION —

search_term = “594” # Example: Penal Code section for vandalism
csv_filename = “penal_code_results.csv”
browser_driver_path = “path_to_chromedriver” # Replace with your actual chromedriver path

— SETUP —

options = webdriver.ChromeOptions()
options.add_argument(“–headless”) # Optional: headless mode
service = Service(browser_driver_path)
driver = webdriver.Chrome(service=service, options=options)

— NAVIGATE TO SITE —

driver.get(“https://leginfo.legislature.ca.gov/faces/codesTextSearch.xhtml”)
time.sleep(2)

— SELECT PENAL CODE FROM DROPDOWN —

dropdown = Select(driver.find_element(By.ID, “codeSearchForm:codeList_input”))
dropdown.select_by_visible_text(“Penal Code”)
time.sleep(1)

— ENTER SEARCH TERM —

search_box = driver.find_element(By.ID, “codeSearchForm:searchText_input”)
search_box.clear()
search_box.send_keys(search_term)
search_box.send_keys(Keys.RETURN)
time.sleep(3)

— EXTRACT RESULTS —

results = driver.find_elements(By.CSS_SELECTOR, “.ui-panel-content > div”)
data = []

for r in results:
try:
text = r.text.strip()
if text:
data.append([text])
except Exception as e:
print(“Error extracting a result:”, e)

— SAVE TO CSV —

with open(csv_filename, mode=’w’, newline=”, encoding=’utf-8′) as f:
writer = csv.writer(f)
writer.writerow([“Penal Code Result”])
writer.writerows(data)

driver.quit()
print(f”✅ Done! Saved {len(data)} results to {csv_filename}”)from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import Select
import time
import csv

— CONFIGURATION —

search_term = “594” # Example: Penal Code section for vandalism
csv_filename = “penal_code_results.csv”
browser_driver_path = “path_to_chromedriver” # Replace with your actual chromedriver path

— SETUP —

options = webdriver.ChromeOptions()
options.add_argument(“–headless”) # Optional: headless mode
service = Service(browser_driver_path)
driver = webdriver.Chrome(service=service, options=options)

— NAVIGATE TO SITE —

driver.get(“https://leginfo.legislature.ca.gov/faces/codesTextSearch.xhtml”)
time.sleep(2)

— SELECT PENAL CODE FROM DROPDOWN —

dropdown = Select(driver.find_element(By.ID, “codeSearchForm:codeList_input”))
dropdown.select_by_visible_text(“Penal Code”)
time.sleep(1)

— ENTER SEARCH TERM —

search_box = driver.find_element(By.ID, “codeSearchForm:searchText_input”)
search_box.clear()
search_box.send_keys(search_term)
search_box.send_keys(Keys.RETURN)
time.sleep(3)

— EXTRACT RESULTS —

results = driver.find_elements(By.CSS_SELECTOR, “.ui-panel-content > div”)
data = []

for r in results:
try:
text = r.text.strip()
if text:
data.append([text])
except Exception as e:
print(“Error extracting a result:”, e)

— SAVE TO CSV —

with open(csv_filename, mode=’w’, newline=”, encoding=’utf-8′) as f:
writer = csv.writer(f)
writer.writerow([“Penal Code Result”])
writer.writerows(data)

driver.quit()
print(f”✅ Done! Saved {len(data)} results to {csv_filename}”)