如何使用 R 从网络下载 pdf 文件(编码问题)
how to download pdf file with R from web (encode issue)
我正在尝试使用 R 从网站下载 pdf 文件。当我尝试使用函数 browserURL 时,它仅适用于参数 encodeIfNeeded = T。结果,如果我传递相同的 url 到函数 download.file,它 returns "cannot open destfile 'downloaded/teste.pdf', reason 'No such file or directory",即它找不到正确的 url.
如何更正编码,以便能够以编程方式下载文件?
我需要自动执行此操作,因为要下载的文件超过一千个。
这是一个最小的可重现代码:
library(tidyverse)
library(rvest)
url <- "http://www.ouvidoriageral.sp.gov.br/decisoesLAI.html"
webpage <- read_html(url)
# scrapping hyperlinks
links_decisoes <- html_nodes(webpage,".borderTD a") %>%
html_attr("href")
# creating full/correct url
full_links <- paste("http://www.ouvidoriageral.sp.gov.br/", links_decisoes, sep="" )
# browseURL only works with encodeIfNeeded = T
browseURL(full_links[1], encodeIfNeeded = T,
browser = "C://Program Files//Mozilla Firefox//firefox.exe")
# returns an error
download.file(full_links[1], "downloaded/teste.pdf")
这里有几个问题。首先,一些文件的链接没有正确地格式化为 url——它们包含空格和其他特殊字符。为了转换它们,您必须使用 url_escape()
,它应该可供您使用,因为加载 rvest 还会加载包含 url_escape()
的 xml2。
其次,您要保存到的路径是相对于您的 R 主目录的,但您并没有告诉 R。您要么需要像这样的完整路径:"C://Users/Manoel/Documents/downloaded/testes.pdf"
,要么像这样的相对路径:path.expand("~/downloaded/testes.pdf")
.
这段代码应该可以满足您的需求:
library(tidyverse)
library(rvest)
# scraping hyperlinks
full_links <- "http://www.ouvidoriageral.sp.gov.br/decisoesLAI.html" %>%
read_html() %>%
html_nodes(".borderTD a") %>%
html_attr("href") %>%
url_escape() %>%
{paste0("http://www.ouvidoriageral.sp.gov.br/", .)}
# Looks at page in firefox
browseURL(full_links[1], encodeIfNeeded = T, browser = "firefox.exe")
# Saves pdf to "downloaded" folder if it exists
download.file(full_links[1], path.expand("~/downloaded/teste.pdf"))
我正在尝试使用 R 从网站下载 pdf 文件。当我尝试使用函数 browserURL 时,它仅适用于参数 encodeIfNeeded = T。结果,如果我传递相同的 url 到函数 download.file,它 returns "cannot open destfile 'downloaded/teste.pdf', reason 'No such file or directory",即它找不到正确的 url.
如何更正编码,以便能够以编程方式下载文件? 我需要自动执行此操作,因为要下载的文件超过一千个。
这是一个最小的可重现代码:
library(tidyverse)
library(rvest)
url <- "http://www.ouvidoriageral.sp.gov.br/decisoesLAI.html"
webpage <- read_html(url)
# scrapping hyperlinks
links_decisoes <- html_nodes(webpage,".borderTD a") %>%
html_attr("href")
# creating full/correct url
full_links <- paste("http://www.ouvidoriageral.sp.gov.br/", links_decisoes, sep="" )
# browseURL only works with encodeIfNeeded = T
browseURL(full_links[1], encodeIfNeeded = T,
browser = "C://Program Files//Mozilla Firefox//firefox.exe")
# returns an error
download.file(full_links[1], "downloaded/teste.pdf")
这里有几个问题。首先,一些文件的链接没有正确地格式化为 url——它们包含空格和其他特殊字符。为了转换它们,您必须使用 url_escape()
,它应该可供您使用,因为加载 rvest 还会加载包含 url_escape()
的 xml2。
其次,您要保存到的路径是相对于您的 R 主目录的,但您并没有告诉 R。您要么需要像这样的完整路径:"C://Users/Manoel/Documents/downloaded/testes.pdf"
,要么像这样的相对路径:path.expand("~/downloaded/testes.pdf")
.
这段代码应该可以满足您的需求:
library(tidyverse)
library(rvest)
# scraping hyperlinks
full_links <- "http://www.ouvidoriageral.sp.gov.br/decisoesLAI.html" %>%
read_html() %>%
html_nodes(".borderTD a") %>%
html_attr("href") %>%
url_escape() %>%
{paste0("http://www.ouvidoriageral.sp.gov.br/", .)}
# Looks at page in firefox
browseURL(full_links[1], encodeIfNeeded = T, browser = "firefox.exe")
# Saves pdf to "downloaded" folder if it exists
download.file(full_links[1], path.expand("~/downloaded/teste.pdf"))