Skip to content

Instantly share code, notes, and snippets.

@apoorv74
Created March 12, 2017 04:29
Show Gist options
  • Save apoorv74/8262c51414d49926bc18c5e508416f3d to your computer and use it in GitHub Desktop.
Save apoorv74/8262c51414d49926bc18c5e508416f3d to your computer and use it in GitHub Desktop.
Scraping numbers for state elections from election commission website - http://eciresults.nic.in/
library(rvest)
col_names <- c("Constituency","Const. No.", "Leading Candidate",
"Leading Party" ,"Trailing Candidate" ,"Trailing Party", "Margin", "Status")
state_codes <- c("05","14","19","28","24")
ec_master <- c()
for(state in 1:length(state_codes)){
sample_url <- paste0('http://eciresults.nic.in/statewiseS',state_codes[state],'.htm?st=S',state_codes[state])
xpath <- '//*[@id="divACList"]/table[1]'
ec_data <- sample_url %>% read_html() %>% html_nodes(xpath=xpath) %>% html_table(fill = T)
ec_data <- data.frame(ec_data)
total_pages <- ec_data[4,1]
total_pages <- as.numeric(unlist(stringi::stri_extract_all_words(total_pages)))
total_pages <- total_pages[!is.na(total_pages)]
total_pages <- c("",total_pages)
total_pages <- total_pages[1:(length(total_pages) - 1)]
first_data <- ec_data[5:nrow(ec_data),]
names(first_data)[] <- col_names
ec_master <- c()
for(i in 1:length(total_pages)){
url <- paste0('http://eciresults.nic.in/StatewiseS',state_codes[state],total_pages[i],'.htm')
ec_data <- url %>% read_html() %>% html_nodes(xpath=xpath) %>% html_table(fill = T)
ec_data <- data.frame(ec_data)
ec_data <- ec_data[5:nrow(ec_data),]
names(ec_data)[] <- col_names
ec_master <- rbind(ec_master,ec_data)
}
ec_master <- unique(ec_master)
ec_master <- ec_master[!is.na(ec_master$`Const. No.`),]
assign(paste0('state_code_',state),ec_master)
write.csv(ec_master,paste0('~/Downloads/state_',state_codes[state],'.csv'),row.names = F)
}
# Candidate wise ----------------------------------------------------------
ec_meta <- data.frame(state_code = c(rep("05",40),rep("14",60),
rep('19',117),rep("24",403),
rep("28",70)),"con_id" = c(seq(1:40),seq(1:60),seq(1:117),
seq(1:403),seq(1:70)))
candidate_master <- c()
for(i in 1:nrow(ec_meta)){
url_f <- paste0('http://eciresults.nic.in/ConstituencywiseS',ec_meta$state_code[i],ec_meta$con_id[i],'.htm?ac=',ec_meta$con_id[i])
xpath <- '//*[@id="div1"]/table[1]'
data_con <- url_f %>% read_html() %>% html_nodes(xpath=xpath) %>% html_table()
data_con <- data.frame(data_con)
data_con$constituency <- data_con[1,1]
data_con <- data_con[4:nrow(data_con),]
names(data_con)[] <- c('candidate','party','votes','constituency')
candidate_master <- rbind(candidate_master,data_con)
}
write.csv(candidate_master,'~/Downloads/candidate_master_master.csv',row.names=F)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment