I’m trying to scrape some tables (election data) using the XML package. Browsing SO, I found out how to scrape a single url using:
library(XML)
url <- "http://www.elecciones2011.gob.ar/paginas/paginas/dat99/DPR99999A.htm"
total <- readHTMLTable(url)
n.rows <- unlist(lapply(total, function(t) dim(t)[1]))
df<-as.data.frame(total[[which.max(n.rows)]])
With the above code I get a nice enough result. I’m also able (with the readLines function and some tweaking) to get a vector with all the urls I want to scrape. Like this:
base_url <- "http://www.elecciones2011.gob.ar/paginas/paginas/"
urls <- paste(
base_url,
c(
"dat02/DPR02999A",
"dat03/DPR03999A",
"dat04/DPR04999A",
"dat05/DPR05999A",
"dat06/DPR06999A",
"dat07/DPR07999A",
"dat08/DPR08999A",
"dat09/DPR09999A",
"dat10/DPR10999A",
"dat11/DPR11999A",
"dat12/DPR12999A",
"dat13/DPR13999A",
"dat14/DPR14999A",
"dat15/DPR15999A",
"dat16/DPR16999A",
"dat17/DPR17999A",
"dat18/DPR18999A",
"dat19/DPR19999A",
"dat20/DPR20999A",
"dat21/DPR21999A",
"dat22/DPR22999A",
"dat23/DPR23999A",
"dat24/DPR24999A"
),
".htm",
sep = ""
)
What I’d like to do is to create a function that runs the readHTMLTable function in all the urls and store the results in a vector or data frame (in one or many, whatever is easier). I’m quite new with R, and I’m particularly bad at functions. I tried something like…
tabla<- for (i in urls){
readHTMLTable(urls)
}
…but it’s not even close.
The most basic approach, using a loop. This just wraps the code you supplied inside a
for.A more elegant approach, using
lapply. Now the code supplied is put inside a function, which is called for each url.