R. download.file using link with special character (ñ) - Linux vs Windows



我需要下载URL中包含的zip文件。如果您只是复制和粘贴即可在浏览器中完美运行。下面的代码在 Linux 中使用,它也运行良好。

link <- "http://www.catastro.minhap.es/INSPIRE/Buildings/32/32035-A GUDIÑA/A.ES.SDGC.BU.32035.zip"
tempzip <- "./temp/"
download.file(link, destfile = tempzip, method = "auto")

现在我也想在 Windows 中下载该文件,但它不起作用。这是Windows中的错误:

> download.file(link, destfile = tempzip, method = "auto")
trying URL 'http://www.catastro.minhap.es/INSPIRE/Buildings/32/32035-A GUDIÑA/A.ES.SDGC.BU.32035.zip'
Error in download.file(ziplink, destfile = tempzip, method = "auto") : 
cannot open URL 'http://www.catastro.minhap.es/INSPIRE/Buildings/32/32035-A GUDIÑA/A.ES.SDGC.BU.32035.zip'
In addition: Warning message:
In download.file(ziplink, destfile = tempzip, method = "auto") :
cannot open URL 'http://www.catastro.minhap.es/INSPIRE/Buildings/32/32035-A GUDIÑA/A.ES.SDGC.BU.32035.zip': HTTP status was '404 Not Found'

如何使用在同一链接的 Windows 和 Linux 中同时有效的函数?

您可以使用URLencode转换为应独立于平台的 url 转义字符串。在 Windows PC 上获得了以下结果:

download.file(URLencode(link), destfile = "myfile.zip", method = "auto")
#> trying URL 'http://www.catastro.minhap.es/INSPIRE/Buildings/32/32035-
#> A%20GUDI%D1A/A.ES.SDGC.BU.32035.zip'
#> Content type 'application/x-zip-compressed' length 706283 bytes (689 KB)
#> downloaded 689 KB

最新更新