How to download multiple urls in R?

2

I need to extract several text files that are organized into annual folders in the following url: link

How do I extract multiple folders at the same time? For example, if I want the 2003, 2004, and 2005 files, so I do not need to write the same code three times.

Thank you!

    
asked by anonymous 29.05.2014 / 02:01

1 answer

2

There are several ways; a not very elegant:

library(RCurl)
library(XML)

base = 'http://www.rsssfbrasil.com/tablesae/'

page = url(base)

download.file(base, destfile='test.html')
page = htmlTreeParse('test.html', useInternal=TRUE, asTree=TRUE)
links = xpathSApply(page, "//a", xmlGetAttr, name='href')

# pega apenas os links com 'htm'
links = links[grep('htm', links)]

for(link in links) {

  download.file(paste(base, link, sep=""), destfile=link)

}
    
30.05.2014 / 16:48