To import the data on the Social Security Scoreboard, infographic of the Estadão site and export to Excel, use the code below.
If you have not installed the 'XML', 'xlsx' and 'stringr' packages, run the first line.
install.packages(c('XML', 'xlsx', 'stringr'))
library(XML)
library(stringr)
library(xlsx)
url <- 'http://infograficos.estadao.com.br/especiais/placar/votacao/economia/?id=GLwN7vXR3W'
paginavoto <- htmlParse(url)
tipo <- xpathSApply(paginavoto, "//section//h3", fun = xmlValue)
deputados <- data.frame(nome = character(),
partido = character(),
voto = character())
for(i in 1:length(tipo)){
if(as.numeric(str_extract(tipo[i], '\\d+')) != 0){
pDep <- paste0("//section[",i ,"]//span[@class='p-name']")
pPart <- paste0("//section[",i ,"]//span[@class='p-org']")
deputado <- data.frame(nome = xpathSApply(paginavoto, pDep, fun = xmlValue),
partido = xpathSApply(paginavoto, pPart, fun = xmlValue),
voto = trimws(str_extract(tipo[i], '\\D+')))
deputados <- rbind(deputados, deputado)
}
}
write.xlsx(deputados, "deputados.xlsx")
mto good, the nodes you picked up comes from the site itself?
– Danilo Imbimbo
Yes. The CSS gadget selector helps in the task of finding them.
– Tomás Barcellos