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.

01library(RSelenium)
02library(tidyverse)
03library(rvest)
04 
05rm(list=ls())
06 
07rs <- rsDriver(
08  remoteServerAddr='localhost',
09  port = netstat::free_port(random=T),
10  browser = 'firefox',
11  # uncomment to run headless
12  #extraCapabilities = list("moz:firefoxOptions" = list(args = list('--headless'))),
13  verbose = F
14)
15 
16# navigate to member search
17rsc <- rs$client
19 
20# a file with the first field is street, second field is city, third field is zip
21addresses <- read_csv('~/Desktop/addresses.csv')
22 
23# iterate through the list of addresses using firefox under selenium control
24for (i in seq(1, nrow(addresses))) {
25  rsc$findElement(using='css', '#gmap_street')$clearElement()
26  rsc$findElement(using='css', '#gmap_street')$sendKeysToElement(list(as.character(addresses[i,1])))
27  rsc$findElement(using='css', '#gmap_city')$clearElement()
28  rsc$findElement(using='css', '#gmap_city')$sendKeysToElement(list(as.character(addresses[i,2])))
29  rsc$findElement(using='css', '#gmap_zip')$clearElement()
30  rsc$findElement(using='css', '#gmap_zip')$sendKeysToElement(list(as.character(addresses[i,3])))
31  rsc$findElement(using='xpath', '//*[@id="form_submit"]')$clickElement()
32   
33  html <- rsc$getPageSource() %>% unlist() %>% read_html()
34   
35  Sys.sleep(0.5)
36   
37  addresses[i,'member'] <- html %>% html_element('#mem-name a') %>% html_text() %>% str_trim()
38  addresses[i,'dist'] <- html %>% html_element('#mem-dist') %>% html_text()  %>% parse_number()
39}
40 
41# write out to csv file
42write_csv(addresses, '~/Desktop/ad_addresses.csv')

Leave a Reply

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