【问题标题】:Download file from url with no specified filename从没有指定文件名的 url 下载文件
【发布时间】:2018-12-10 09:32:08
【问题描述】:

我想从未指定文件名的 url 下载文件。如果我在浏览器中输入这个 url (https://www.cpr.dk/vejregister),它会下载正确的文件。

如果我编写此代码,则只下载标头:

# In R
download.file(url = "https://www.cpr.dk/vejregister",
              destfile = "/data/kkgrunddata/Vejdata/vejdata_latest",
              method='curl')

# In BASH
>more vejdata_latest

<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href="/media/21570/vejregister_hele_landet_pr_181201.zip">here</a>.</h2>
</body></html>

您对如何在不从标题中指定文件名的情况下下载文件有任何建议吗?

【问题讨论】:

  • 我的猜测是某些 javascript(或其他语言?)正在接受您的点击,然后将文件传递给下载。如果是这种情况,您可以使用例如phantomjs在网页上执行javascript并获取文件?

标签: r curl download


【解决方案1】:

我能够用您的代码重现该问题。删除 method='curl' 参数后,我无需在标题中指定链接即可下载它。

download.file(url = "https://www.cpr.dk/vejregister",
          destfile = "./vejdata_latest")

编辑:如果您使用的是 Windows,请不要忘记文件扩展名(在本例中为 .zip)

【讨论】:

  • 没有。如果您使用的是 Windows,则需要添加扩展名 (.zip)。 UNIX 系统自动识别文件类型。我的 R 会话也识别它:Content type 'application/x-zip-compressed'。不过好点,会更新答案。
  • 啊,那是有道理的。我只是以为是字节或其他东西:P
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-16
  • 1970-01-01
  • 2021-12-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多