简体   繁体   English

使用 purrr 下载多个文件

[英]Downloading multiple files using purrr

I am trying to download all of the Excel files at: https://www.grants.gov.au/reports/gaweeklyexport我正在尝试下载所有 Excel 文件: https://www.grants.gov.au/reports/gaweeklyexport

Using the code below I get errors similar to the following for each link (77 in total):使用下面的代码,每个链接都会出现类似于以下的错误(总共 77 个):

[[1]]$error
<simpleError in download.file(.x, .y, mode = "wb"): scheme not supported in URL '/Reports/GaWeeklyExportDownload?GaWeeklyExportUuid=0db183a2-11c6-42f8-bf52-379aafe0d21b'>

I get this error when trying to iterate over the full list, but when I call download.file on an individual list item it works fine.尝试遍历整个列表时出现此错误,但是当我在单个列表项上调用 download.file 时它工作正常。

I would be grateful if someone could tell me what I have done wrong or suggest a better way of doing it.如果有人能告诉我我做错了什么或提出更好的方法,我将不胜感激。

The code that produces the error:产生错误的代码:


library(tidyverse)
library(rvest)

# Reading links to the Excel files to be downloaded
url <- "https://www.grants.gov.au/reports/gaweeklyexport"

webpage <- read_html(url)

# The list of links to the Excel files
links <- html_attr(html_nodes(webpage, '.u'), "href")

# Creating names for the files to supply to the download.file function
wb_names = str_c(1:77, ".xlsx")

# Defining a function that using purrr's safely to ensure it doesn't fail if there is a dead link
safe_download <- safely(~ download.file(.x , .y, mode = "wb"))

# Combining links, file names, and the function returns an error
map2(links, wb_names, safe_download)

You need to prepend 'https://www.grants.gov.au/' to the URL to get the absolute path of the file which can be used to download the file.您需要在 URL 前添加'https://www.grants.gov.au/'以获得可用于下载文件的文件的绝对路径。

library(rvest)
library(purrr)

url <- "https://www.grants.gov.au/reports/gaweeklyexport"

webpage <- read_html(url)
# The list of links to the Excel files
links <- paste0('https://www.grants.gov.au/', html_attr(html_nodes(webpage, '.u'), "href"))

safe_download <- safely(~ download.file(.x , .y, mode = "wb"))

# Creating names for the files to supply to the download.file function
wb_names = paste0(1:77, ".xlsx")
map2(links, wb_names, safe_download)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM