【问题标题】:Down load data from HTTPS in R在 R 中从 HTTPS 下载数据
【发布时间】:2021-12-31 15:42:29
【问题描述】:

我在 R 中从 HTTPS 下载数据时遇到问题,我尝试使用 curl,但它不起作用。

URL <- "https://github.com/Bitakhparsa/Capstone/blob/0850c8f65f74c58e45f6cdb2fc6d966e4c160a78/Plant_1_Generation_Data.csv"

options('download.file.method'='curl')
download.file(URL, destfile = "./data.csv", method="auto")

我下载了包含该代码的 CSV 文件,但是当我检查数据时格式发生了变化。所以没有正确下载。 请问有人帮帮我吗?

【问题讨论】:

  • 您收到的错误是什么?
  • data.table 中的 fread() 函数非常适合下载数据文件,只需将 url 字符串直接插入其中:fread("https://git....

标签: r https download


【解决方案1】:

我认为您实际上可能有错误的 URL。我想你想要:

https://raw.githubusercontent.com/Bitakhparsa/Capstone/0850c8f65f74c58e45f6cdb2fc6d966e4c160a78/Plant_1_Generation_Data.csv

然后你可以直接使用library(RCurl)下载文件,而不是用URL创建变量

library(RCurl)
download.file("https://raw.githubusercontent.com/Bitakhparsa/Capstone/0850c8f65f74c58e45f6cdb2fc6d966e4c160a78/Plant_1_Generation_Data.csv",destfile="./data.csv",method="libcurl")

您也可以使用以下命令将文件直接从站点加载到 R 中

URL <- "https://github.com/Bitakhparsa/Capstone/blob/0850c8f65f74c58e45f6cdb2fc6d966e4c160a78/Plant_1_Generation_Data.csv"
out <- read.csv(textConnection(URL))

【讨论】:

  • 非常感谢,它有效!
  • 如果解决方案适合您,请随时接受答案
【解决方案2】:

您可以使用“raw.githubusercontent.com”链接,即在浏览器中,当您转到“https://github.com/Bitakhparsa/Capstone/blob/0850c8f65f74c58e45f6cdb2fc6d966e4c160a78/Plant_1_Generation_Data.csv”时,您可以点击链接“查看原始文件”(它在“抱歉,但我们现在无法显示这么大的文件”上方。)这会将您带到实际数据。您还有一些小错别字。

这对我来说按预期工作:

url <- "https://raw.githubusercontent.com/Bitakhparsa/Capstone/0850c8f65f74c58e45f6cdb2fc6d966e4c160a78/Plant_1_Generation_Data.csv"
download.file(url, destfile = "./data.csv", method="auto")
df <- read.csv("~/Desktop/data.csv")

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-12
  • 2015-02-10
  • 1970-01-01
  • 2016-07-13
相关资源
最近更新 更多