gpt4 book ai didi

r - 隐藏最终链接时,下载一个保留原始文件名的文件

转载 作者:行者123 更新时间:2023-12-04 17:34:12 25 4
gpt4 key购买 nike

我需要下载文件,将其保存在文件夹中,同时保留网站中的原始文件名。

url <- "http://www.seg-social.es/prdi00/idcplg?IdcService=GET_FILE&dID=187112&dDocName=197533&allowInterrupt=1"

从Web浏览器中,如果单击该链接,则可以下载具有以下文件名的excel文件:

AfiliadosMuni-02-2015.xlsx



我知道我可以使用R中的命令download.file轻松下载它,如下所示:
download.file(url, "test.xlsx", method = "curl")

但是我的脚本真正需要的是下载它,以保持原始文件名不变。我也知道我可以像这样从我的控制台使用curl来做到这一点。
curl -O -J $"http://www.seg-social.es/prdi00/idcplg?IdcService=GET_FILE&dID=187112&dDocName=197533&allowInterrupt=1"

但是,再次,我需要在R脚本中使用它。有没有一种类似于上面的方法,但是在R中?我已经研究了RCurl包,但是找不到解决方案。

最佳答案

您可以随时执行以下操作:

library(httr)
library(stringr)

# alternate way to "download.file"
fil <- GET("http://www.seg-social.es/prdi00/idcplg?IdcService=GET_FILE&dID=187112&dDocName=197533&allowInterrupt=1",
write_disk("tmp.fil"))
# get what name the site suggests it shld be
fname <- str_match(headers(fil)$`content-disposition`, "\"(.*)\"")[2]
# rename
file.rename("tmp.fil", fname)

关于r - 隐藏最终链接时,下载一个保留原始文件名的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29017992/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com