这是关于如何报废此页面的基本思路。虽然如果有很多页面要报废,它可能会很慢。
现在你的问题有点模棱两可。您希望最终结果是 .txt 文件。有pdf的网页呢???好的。您仍然可以使用此代码并将具有 pdf 的网页的文件扩展名更改为 pdf。
library(xml2)
library(rvest)
urll="https://search.newyorkfed.org/board_public/search?start=10&Search=&number=10&text=inflation"
urll%>%read_html()%>%html_nodes("div#results a")%>%html_attr("href")%>%
.[!duplicated(.)]%>%lapply(function(x) read_html(x)%>%html_nodes("body"))%>%
Map(function(x,y) write_html(x,tempfile(y,fileext=".txt"),options="format"),.,
c(paste("tmp",1:length(.))))
这是上面代码的细分:
您要从中删除的 url:
urll="https://search.newyorkfed.org/board_public/search?start=10&Search=&number=10&text=inflation"
获取所有你需要的url:
allurls <- urll%>%read_html()%>%html_nodes("div#results a")%>%html_attr("href")%>%.[!duplicated(.)]
你想在哪里保存你的文本?创建临时文件:
tmps <- tempfile(c(paste("tmp",1:length(allurls))),fileext=".txt")
按照现在。您的 allurls 是班级角色。您必须将其更改为 xml 才能废弃它们。然后最后将它们写入上面创建的 tmp 文件中:
allurls%>%lapply(function(x) read_html(x)%>%html_nodes("body"))%>%
Map(function(x,y) write_html(x,y,options="format"),.,tmps)
请不要遗漏任何内容。例如在..."format"), 之后有一个句点。考虑到这一点。
现在您的文件已写入 tempdir。要确定它们的位置,只需在控制台上键入命令tempdir(),它就会为您提供文件的位置。同时,您可以在tempfile 命令中更改报废文件的位置。
希望这会有所帮助。