【发布时间】:2015-07-31 08:32:42
【问题描述】:
如何使用 R 将 href 更改为有意义的 URL?有意义的是,我理解一个地址,如果粘贴到浏览器将正确打开。
例如:
<a href="../../systemfit/html/systemfit.html">systemfit</a>
读自: http://artax.karlin.mff.cuni.cz/r-help/library/systemfit/html/systemfit.control.html
进入: http://artax.karlin.mff.cuni.cz/r-help/library/systemfit/html/systemfit.html
我做的是:
collectLinks <- function(x){
library(stringi)
fileUrl <- (x)
html <- paste(readLines(fileUrl, warn=FALSE), collapse="\n")
matched <- stri_match_all_regex(html, "<a href=\"(.*?)\"")
matched[[1]][, 2]
}
links <- collectLinks("http://artax.karlin.mff.cuni.cz/r-help/library/systemfit/html/systemfit.control.html")
函数 collectLinks 将包含 URL 的字符串作为输入。它返回在 x 上找到的 href 内容的字符向量。
接下来我想做的是遍历链接中的每个元素并从中提取 href 内容。然而:
[1] "../../systemfit/html/systemfit.html" "../../systemfit/html/solve.html"
[3] "../../systemfit/html/det.html" "../../systemfit/html/systemfit.html"
[5] "mailto:arne.henningsen@googlemail.com" "../../systemfit/html/systemfit.html"
[7] "00Index.html"
不是有意义的 URL。
readLines(links[1])
Error in file(con, "r") : cannot open the connection
In addition: Warning message:
In file(con, "r") :
cannot open file '../../systemfit/html/systemfit.html': No such file or directory
我想知道是否有一种通用方法可以将 a href 内容转换为可以进一步利用的有意义的 URL?
【问题讨论】:
-
我不太明白你的意思?你可能只需要一些正则表达式??只需删除开头和结尾的 和 sub
(">systemfit</a>, "")?? -
我正在使用 readLines 读取 html 并使用正则表达式提取 a href 内容。接下来我想做的是从href打开内容,所以结果来自正则表达式,但这对于readLines来说不是一个有意义的url。