给出以下示例:
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
Run Code Online (Sandbox Code Playgroud)
如何应用一个正则表达式来解析html片段以提取网址和链接文本并将它们弹出到数据框中的单独列中?例如,在上面的示例中,为了生成如下所示的数据框,我需要做些什么:
url name sites
http://example.com/path This website site 1
http://example.com/path2 That website site 2
Run Code Online (Sandbox Code Playgroud)
这是一个使用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"
Run Code Online (Sandbox Code Playgroud)