我是 html
新手,但正在使用脚本来下载给定网页链接到的所有 PDF
文件(为了好玩并避免无聊的手动工作)并且我无法在 html
文档中找到我应该查找完成相对路径的数据 - 我知道这是可能的,因为我的网络浏览器可以做到这一点。
示例:我试图抓取链接到 this page from ocw.mit.edu 的讲义使用R
包rvest
查看原始html
或访问a
的href
属性>“节点”我只得到相对路径:
library(rvest)
url <- paste0("https://ocw.mit.edu/courses/",
"electrical-engineering-and-computer-science/",
"6-006-introduction-to-algorithms-fall-2011/lecture-notes/")
# Read webpage and extract all links
links_all <- read_html(url) %>%
html_nodes("a") %>%
html_attr("href")
# Extract only href ending in "pdf"
links_pdf <- grep("pdf$", tolower(links_all), value = TRUE)
links_pdf[1]
[1] "/courses/electrical-engineering-and-computer-science/6-006-introduction-to-algorithms-fall-2011/lecture-videos/mit6_006f11_lec01.pdf"
最佳答案
我今天发现的最简单的解决方案是使用 xml2
包的 url_absolute(x, base)
函数。对于基本参数,您可以使用从中检索源的页面的 URL。
这似乎比尝试通过正则表达式提取地址的基本 URL 更不容易出错。
关于html - 使用 R 查找给定相对 href 的绝对 html 路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47498461/