从R中的网页返回链接列表

时间:2011-09-23 16:51:26

标签: xml r web-scraping

我正在尝试在r中编写一个函数,给定一个地址,将返回该网页上的链接列表。

例如:

getLinks("http://prog21.dadgum.com/109.html")

会回来:

"http://prog21.dadgum.com/prog21.css"
"http://prog21.dadgum.com/atom.xml"
"http://prog21.dadgum.com/index.html"
"http://prog21.dadgum.com/archives.html"
"http://prog21.dadgum.com/atom.xml"
"http://prog21.dadgum.com/56.html"
"http://prog21.dadgum.com/39.html"
"http://prog21.dadgum.com/109.html"
"http://prog21.dadgum.com/108.html"
"http://prog21.dadgum.com/107.html"
"http://prog21.dadgum.com/106.html"
"http://prog21.dadgum.com/105.html"
"http://prog21.dadgum.com/104.html"

1 个答案:

答案 0 :(得分:3)

此功能似乎适用于其他网页,但由于某些原因,不会返回相关网页的完整网址。我很想知道是否有更好的方法来做到这一点。

getLinks <- function(URL) {
    require(XML)
    doc <- htmlParse(URL)
    out <- unlist(doc['//@href'])
    names(out) <- NULL
    out
}
相关问题