Jelo A
Jelo A

Reputation: 39

Error when downloading multiple pdf files from list of urls in R

I have a list of urls and trying to download the pdfs they link to with lapply. Even though the download bar pops up, I get the following message and the files are not downloaded:

only first element of 'destfile' argument usedtrying URL 'https://reliefweb.int/sites/reliefweb.int/files/resources/hno_car_2021_final_fr.pdf' Content type 'application/pdf' length 22087482 bytes (21.1 MB) downloaded 21.1 MB

names<- lapply(pdf, basename) # get names
destination<- paste0 ("~/", names)
lapply(pdf,download.file, destfile=destination)
pdf
[[1]]
[1] "https://reliefweb.int/sites/reliefweb.int/files/resources/hno_car_2021_final_fr.pdf"

[[2]]
[1] "https://reliefweb.int/sites/reliefweb.int/files/resources/rnro_centralsahel_oct_2020_fr_web.pdf"

[[3]]
[1] "https://reliefweb.int/sites/reliefweb.int/files/resources/rnro_centralsahel_oct_2020_en_web.pdf"

[[4]]
[1] "https://reliefweb.int/sites/reliefweb.int/files/resources/hno_2020-final.pdf"

[[5]]
[1] "https://reliefweb.int/sites/reliefweb.int/files/resources/hno_light_2020-en_final_0.pdf"

[[6]]
[1] "https://reliefweb.int/sites/reliefweb.int/files/resources/20200701_HNO_CENTROAMERICA%20ADDENDUM%20ING.pdf"

[[7]]
[1] "https://reliefweb.int/sites/reliefweb.int/files/resources/20200706%20ONEPAGER%20HNO%20Centroame%CC%81rica%20ING.pdf"

[[8]]
[1] "https://reliefweb.int/sites/reliefweb.int/files/resources/cmr_hno_2020-revised_print.pdf"

[[9]]
[1] "https://reliefweb.int/sites/reliefweb.int/files/resources/20200616_HNO_CENTROAMERICA%20ADDENDUM.pdf"

Upvotes: 0

Views: 402

Answers (1)

Liman
Liman

Reputation: 1300

I would handle everything at once. see code example for the first 2 files.

names = c("https://reliefweb.int/sites/reliefweb.int/files/resources/hno_car_2021_final_fr.pdf",
          "https://reliefweb.int/sites/reliefweb.int/files/resources/rnro_centralsahel_oct_2020_fr_web.pdf"
)


# making the filenames
downloaded = lapply(names, function(url){
  # extract the last part of the url to make the filename
  destination = unlist(strsplit(url, '/'))
  destination = destination[length(destination)]
  destination = paste0 ("~/", destination)
  # download the file
  download.file(url = url, destfile=destination, mode="wb")
  return(destination) # This is optional, just the see where the files are saved
})

# downloaded

# [[1]]
# [1] "~/hno_car_2021_final_fr.pdf"

# [[2]]
# [1] "~/rnro_centralsahel_oct_2020_fr_web.pdf"

Upvotes: 2

Related Questions