我想使用https://api.c3.ai/covid/api/1/linelistrecord/fetch
API,但只能获取2000条记录。我知道有超过2000条记录-如何获取它们?
这是我在R中的代码:
library(tidyverse)
library(httr)
library(jsonlite)
resp <- POST(
"https://api.c3.ai/covid/api/1/linelistrecord/fetch",
body = list(
spec = {}
) %>% toJSON(auto_unbox = TRUE),
accept("application/json")
)
length(content(resp)$objs)
我得到2000条记录。
将spec
要传递的有以下可选字段,其中包括:
limit
//要返回的最大对象数offset
//用于分页读取的偏移量默认值为limit
2000。
返回的获取结果具有一个布尔字段以及称为的对象数组hasMore
,该对象指示基础数据存储区中是否还有更多记录。
您可以编写一个循环,该循环一旦hasMore
为假就结束。以offset
0开头,并限制n
(例如n=2000
),然后将偏移量迭代增加n
。
library(tidyverse)
library(httr)
library(jsonlite)
limit <- 2000
offset <- 0
hasMore <- TRUE
all_objs <- c()
while(hasMore) {
resp <- POST(
"https://api.c3.ai/covid/api/1/linelistrecord/fetch",
body = list(
spec = list(
limit = limit,
offset = offset,
filter = "contains(location, 'California')" # just as an example, to cut down on the dataset
)
) %>% toJSON(auto_unbox = TRUE),
accept("application/json")
)
hasMore <- content(resp)$hasMore
offset <- offset + limit
all_objs <- c(all_objs, content(resp)$objs)
}
length(all_objs)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句