我有一个XMLNodeSet包含带有超链接的 HTML 表格的对象。当我readHTMLTable用来转换为 data.frame 时效果很好,但超链接信息丢失了。有没有办法在包含超链接的 data.frame 中创建一个附加列?
也许一个更简单的例子是从http://stoptb.org/countries/tbteam/reg_wpro.asp的表中提取超链接。
table <- readHTMLTable("http://stoptb.org/countries/tbteam/reg_wpro.asp")
df <- data.frame(table[[8]])
Run Code Online (Sandbox Code Playgroud)
readHTMLTable调用xmlValue作为其默认值elFun。您可以简单地定义一个不同的函数来提取超链接:
require(XML)
regURL <- "http://stoptb.org/countries/tbteam/reg_wpro.asp"
table <- readHTMLTable(regURL, stringsAsFactors = FALSE)
df <- table[[8]]
hrefFun <- function(x){
xpathSApply(x,'./a',xmlAttrs)
}
table2 <- readHTMLTable(regURL, elFun = hrefFun, stringsAsFactors = FALSE)
df2 <- table2[[8]]
df$URLS <- df2$V2
Run Code Online (Sandbox Code Playgroud)