如何在R中读取多个HTML表

时间:2012-08-03 21:50:33

标签: r

我正在尝试自动拉入并保存到这个readHTML函数的数据框;我是一个R新手,我很难弄清楚如何编写一个循环,这将自动化这个功能,如果你一个接一个地工作。

library('XML')

urls<-c("http://www.basketball-reference.com/teams/ATL/","http://www.basketball-reference.com/teams/BOS/")
theurl<-urls[2] #Pick second link (celtics)

tables <- readHTMLTable(theurl)
n.rows <- unlist(lapply(tables, function(t) dim(t)[1]))
BOS <-tables[[which.max(n.rows)]] 
Team.History<-write.csv(BOS,"Bos.csv")

非常感谢任何和所有帮助!

2 个答案:

答案 0 :(得分:2)

我认为这结合了两个答案的最佳结果(并整理了一点)。

library(RCurl)
library(XML)

stem <- "http://www.basketball-reference.com/teams/"
teams <- htmlParse(getURL(stem), asText=T)
teams <- xpathSApply(teams,"//*/a[contains(@href,'/teams/')]", xmlAttrs)[-1]
teams <- gsub("/teams/(.*)/", "\\1", teams)
urls <- paste0(stem, teams)

names(teams) <- NULL   # get rid of the "href" labels
names(urls) <- teams

results <- data.frame()
for(team in teams){
   tables <- readHTMLTable(urls[team])
   n.rows <- unlist(lapply(tables, function(t) dim(t)[1]))
   team.results <- tables[[which.max(n.rows)]] 
   write.csv(team.results, file=paste0(team, ".csv"))
   team.results$TeamCode <- team
   results <- rbind(results, team.results)
   rm(team.results, n.rows, tables)
}
rm(stem, team)

write.csv(results, file="AllTeams.csv")

答案 1 :(得分:1)

我假设你想循环你的网址矢量?我会尝试这样的事情:

library('XML')

url_base <- "http://www.basketball-reference.com/teams/"
teams <- c("ATL", "BOS")

# better still, get the full list of teams as in
# http://stackoverflow.com/a/11804014/1543437

results <- data.frame()
for(team in teams){
   theurl <- paste(url_base, team , sep="/")
   tables <- readHTMLTable(theurl)
   n.rows <- unlist(lapply(tables, function(t) dim(t)[1]))
   team.results <-tables[[which.max(n.rows)]] 
   write.csv(team.results, file=paste0(team, ".csv"))
   team.results$TeamCode <- team
   results <- rbind(results, team.results)
}
write.csv(results, file="AllTeams.csv")