我正在使用来自美国财政部的API数据。我使用了
GET()
包中的 httr
函数和 fromJSON()
包中的 jsonlite
函数。这是一个 API 示例:
library(httr)
library(jsonlite)
url <- https://api.fiscaldata.treasury.gov/services/api/fiscal_service/v2/accounting/od/avg_interest_rates
data1 <- GET(url)
data2 <- fromJSON(rawToChar(data1$content))
data2 <- data.frame((data1$data))
问题是我只能看到前 100 个观察值的数据。输出:
str(data3)
'data.frame': 100 obs. of 11 variables:
$ record_date : chr "2001-01-31" "2001-01-31" "2001-01-31" "2001-01-31" ...
$ security_type_desc : chr "Marketable" "Marketable" "Marketable" "Marketable" ...
$ security_desc : chr "Treasury Notes" "Treasury Bonds" "Treasury Inflation-Indexed Notes" "Treasury Inflation-Indexed Bonds" ...
$ avg_interest_rate_amt : chr "6.096" "8.450" "3.772" "3.866" ...
$ src_line_nbr : chr "2" "3" "4" "5" ...
$ record_fiscal_year : chr "2001" "2001" "2001" "2001" ...
$ record_fiscal_quarter : chr "2" "2" "2" "2" ...
$ record_calendar_year : chr "2001" "2001" "2001" "2001" ...
$ record_calendar_quarter: chr "1" "1" "1" "1" ...
$ record_calendar_month : chr "01" "01" "01" "01" ...
$ record_calendar_day : chr "31" "31" "31" "31" ...
我需要全部可用数据(而不仅仅是前 100 个观察结果),但我无法找出问题所在。 API 本身并不限制或强加数据限制。你能帮我一下吗?谢谢。
正如我上面的评论所暗示的,这是一个服务器端限制。 API 文档位于 https://fiscaldata.treasury.gov/datasets/average-interest-rates-treasury-securities/average-interest-rates-on-u-s-treasury-securities 描述了分页和例如通过请求
url <- paste0("https://api.fiscaldata.treasury.gov/",
"services/api/fiscal_service/v2/accounting/od/avg_interest_rates")
res1 <- RcppSimdJson::fload(paste0(url,"?page[number]=1&page[size]=1000"))
res2 <- RcppSimdJson::fload(paste0(url,"?page[number]=2&page[size]=1000"))
您请求并获得包含 1000 条记录的集合的第一页和第二页。