Automate AD-ing using Assembly’s Official Geocoder

Automate AD-ing using NY Assembly’s Official Geocoder

Just a short and simple RSelenium and Rvest script for grabbing what Assembly District people live in using the Assembly’s official website. An alternative, faster solution for many addresses would be to use the Python script I wrote that uses the State Geocoder and a Spatial Join, but this script will work for shorter lists when you want to ensure you have “official” results from the Assembly website.

library(RSelenium)
library(tidyverse)
library(rvest)

rm(list=ls())

rs <- rsDriver(
  remoteServerAddr='localhost',
  port = netstat::free_port(random=T),
  browser = 'firefox',
  # uncomment to run headless
  #extraCapabilities = list("moz:firefoxOptions" = list(args = list('--headless'))),
  verbose = F
)

# navigate to member search
rsc <- rs$client
rsc$navigate("https://nyassembly.gov/mem/search/")

# a file with the first field is street, second field is city, third field is zip
addresses <- read_csv('~/Desktop/addresses.csv')

# iterate through the list of addresses using firefox under selenium control
for (i in seq(1, nrow(addresses))) {
  rsc$findElement(using='css', '#gmap_street')$clearElement()
  rsc$findElement(using='css', '#gmap_street')$sendKeysToElement(list(as.character(addresses[i,1])))
  rsc$findElement(using='css', '#gmap_city')$clearElement()
  rsc$findElement(using='css', '#gmap_city')$sendKeysToElement(list(as.character(addresses[i,2])))
  rsc$findElement(using='css', '#gmap_zip')$clearElement()
  rsc$findElement(using='css', '#gmap_zip')$sendKeysToElement(list(as.character(addresses[i,3])))
  rsc$findElement(using='xpath', '//*[@id="form_submit"]')$clickElement()
  
  html <- rsc$getPageSource() %>% unlist() %>% read_html()
  
  Sys.sleep(0.5)
  
  addresses[i,'member'] <- html %>% html_element('#mem-name a') %>% html_text() %>% str_trim()
  addresses[i,'dist'] <- html %>% html_element('#mem-dist') %>% html_text()  %>% parse_number()
}

# write out to csv file
write_csv(addresses, '~/Desktop/ad_addresses.csv')

Leave a Reply

Your email address will not be published. Required fields are marked *