从R中的HTML片段中提取URL

时间:2011-09-02 08:32:01

标签: r

给出以下示例:

sites=c('site 1','site 2')
link=c('<a href="http://example.com/path">This website</a>', '<a href="http://example.com/path2">That website</a>')
w=data.frame(link,sites)
w
                                             link      sites
<a href="http://example.com/path">This website</a>   site 1
<a href="http://example.com/path2">That website</a>  site 2

如何应用正则表达式来解析html片段以提取网址和链接文本并将它们弹出到数据框中的单独列中?例如,在上面的示例中,为了生成如下所示的数据框,我需要做些什么:

                     url          name     sites
http://example.com/path    This website   site 1
http://example.com/path2   That website   site 2

2 个答案:

答案 0 :(得分:8)

以下是使用包htmlTreeParse中的XML函数的解决方案(即没有正则表达式)

R> library("XML") 
R> htp <- htmlTreeParse(link)
R> t(sapply(seq_along(link),
+           function(i) c(url=unname(htp$children$html[[1]][[i]]$attributes),
+                         name=htp$children$html[[1]][[i]]$children$text$value,
+                         sites=sites[i])))
     url                        name           sites   
[1,] "http://example.com/path"  "This website" "site 1"
[2,] "http://example.com/path2" "That website" "site 2"

答案 1 :(得分:0)

rvest的替代解决方案:

library(rvest)

sites <- c('site 1','site 2')
link <- c('<a href="http://example.com/path">This website</a>', 
          '<a href="http://example.com/path2">That website</a>')
w <- data.frame(link, sites)

anchors <- html_nodes(read_html(paste0(w$link, collapse="")), "a")

data.frame(url=html_attr(anchors, "href"),
           name=html_text(anchors),
           sites=w$sites)
##                        url         name  sites
## 1  http://example.com/path This website site 1
## 2 http://example.com/path2 That website site 2