0

我正在尝试使用 postForm 从网上下载 csv 数据,但是,下载的文件似乎是 html 文档而不是 csv 文件。这些是我的代码:

############################
library(RCurl)

url <- "https://research.stlouisfed.org/fred2/series/MKTGDPSAA646NWDB/downloaddata"
params <- list(
  "native_frequency" = "Annual", units = "lin",
  "frquency" = "Annual", "aggregation" = "Average", "obs_start_date" = "1968-01-01",
  "obs_end_date" = "2014-01-01", "file_format" = "csv", "download_data_2" = "", format="csv")

result <- postForm( url, .params = params)
writeBin(as.vector(result),"doc2.txt")
4

1 回答 1

0

一种更现代的方法是使用httr包:

library(httr)

res <- POST("https://research.stlouisfed.org/",
            path="fred2/series/MKTGDPSAA646NWDB/downloaddata",
            body=list(`form[native_frequency]` = "Annual", 
                      `form[units]` = "lin", 
                      `form[frequency]` = "Annual", 
                      `form[aggregation]` = "Average", 
                      `form[obs_start_date]` = "1968-01-01", 
                      `form[obs_end_date]` = "2014-01-01", 
                      `form[file_format]` = "csv", 
                      `form[download_data_2]` = ""),
            write_disk("doc2.txt"))

warn_for_status(res)

##         DATE       VALUE
## 1 1968-01-01  4187777711
## 2 1969-01-01  4485777644
## 3 1970-01-01  5377333333
## 4 1971-01-01  7184853037
## 5 1972-01-01  9664157065
## 6 1973-01-01 14947391339
于 2016-02-07T12:55:05.357 回答