【问题标题】:Return a list of links from a webpage in R从R中的网页返回链接列表
【发布时间】:2011-09-23 16:51:25
【问题描述】:

我正在尝试在 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"

【问题讨论】:

    标签: xml r web-scraping


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-04
      • 1970-01-01
      • 2021-05-03
      • 2011-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-08
      相关资源
      最近更新 更多