r - 从 API 响应列表中提取数据

标签 r httr

我从 API 调用中提取数据,如下所示。请记住,dput 中下面 99% 的数据都是无用的,我只需要它来重现这个问题。运行它应该返回如下所示的响应:

    [[1]]
Response [https://test.api.com/v4/12345/information]
  Date: 2021-09-02 20:24
  Status: 204
  Content-Type: <unknown>
<EMPTY BODY>

[[2]]
Response [https://test.api.com/v4/23456/information]
  Date: 2021-09-02 20:24
  Status: 204
  Content-Type: <unknown>
<EMPTY BODY>

我想做的是从这些响应中提取信息,如下所示:

<表类=“s-表”> <标题> 网址 日期 状态 <正文> https://test.api.com/v4/12345/information 21年9月2日20:24 204 https://test.api.com/v4/23456/information 21年9月2日20:24 204

如果这只是一个响应,我可以通过执行 response_blurb$urlresponse_blurb$date 之类的操作来调用此数据,但如果有多个响应,我就无法这样做。

dput 如下:

response_blurb <- list(structure(list(url = "https://test.api.com/v4/12345/information", 
                                  status_code = 204L, headers = structure(list(`cache-control` = "no-cache", 
                                                                               pragma = "no-cache", expires = "-1", `strict-transport-security` = "max-age=31536000", 
                                                                               `x-content-type-options` = "nosniff", `x-xss-protection` = "1; mode=block", 
                                                                               date = "Thu, 02 Sep 2021 20:24:43 GMT", `x-cdn` = "Imperva", 
                                                                               `x-iinfo` = "7-62735819-62735821 NNNY CT(32 73 0) RT(1630614282821 31) q(0 0 0 -1) r(1 1) U6"), class = c("insensitive", 
                                                                                                                                                                                         "list")), all_headers = list(list(status = 204L, version = "HTTP/1.1", 
                                                                                                                                                                                                                           headers = structure(list(`cache-control` = "no-cache", 
                                                                                                                                                                                                                                                    pragma = "no-cache", expires = "-1", `strict-transport-security` = "max-age=31536000", 
                                                                                                                                                                                                                                                    `x-content-type-options` = "nosniff", `x-xss-protection` = "1; mode=block", 
                                                                                                                                                                                                                                                    date = "Thu, 02 Sep 2021 20:24:43 GMT", `x-cdn` = "Imperva", 
                                                                                                                                                                                                                                                    `x-iinfo` = "7-62735819-62735821 NNNY CT(32 73 0) RT(1630614282821 31) q(0 0 0 -1) r(1 1) U6"), class = c("insensitive", 
                                                                                                                                                                                                                                                                                                                                                              "list")))), cookies = structure(list(domain = c("#HttpOnly_.secure", 
                                                                                                                                                                                                                                                                                                                                                                                                              ".secure", ".secure", "#HttpOnly_api.secure"
                                                                                                                                                                                                                                                                                                                                                              ), flag = c(TRUE, TRUE, TRUE, FALSE), path = c("/", "/", 
                                                                                                                                                                                                                                                                                                                                                                                                             "/", "/"), secure = c(TRUE, TRUE, TRUE, FALSE), expiration = structure(c(1661415232, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      Inf, Inf, Inf), class = c("POSIXct", "POSIXt")), name = c("visid_incap_754428", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                "nlbi_754428", "incap_ses_1286_754428", "ASP.NET_SessionId"
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      ), value = c("kpT+BJeYSuGXS5d9taQHzJiYJmEAAAAAQUIPAAAAAAAyrXukfkBb9mpZHnGcr6zX", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   "pprNEnQFZjMz5rnaoK07BAAAAADZLQdRn3tbElZnp5AzMYef", "uTPqYDW0LgRsKI+vbsrYEW0xMWEAAAAAkinWhXc+BLr4Y9HRlIvAtQ==", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   "5ndj041zg1zda2us2ai3eape")), row.names = c(NA, -4L), class = "data.frame"), 
                                  content = raw(0), date = structure(1630614283, class = c("POSIXct", 
                                                                                           "POSIXt"), tzone = "GMT"), times = c(redirect = 0, namelookup = 0.052203, 
                                                                                                                                connect = 0.068757, pretransfer = 0.094581, starttransfer = 0, 
                                                                                                                                total = 0.193515), request = structure(list(method = "POST", 
                                                                                                                                                                            url = "https://test.api.com/v4/12345/information", 
                                                                                                                                                                            headers = c(Accept = "application/json", Authorization = "Basic KFOSJFBfoepsEOTPSDnrfjs395031==", 
                                                                                                                                                                                        `Content-Type` = "application/json"), fields = NULL, 
                                                                                                                                                                            options = list(useragent = "libcurl/7.64.1 r-curl/4.3.2 httr/1.4.2", 
                                                                                                                                                                                           post = TRUE, postfieldsize = 157L, postfields = as.raw(c(0x20
                                                                                                                                                                                                                                                    ))), auth_token = NULL, output = structure(list(), class = c("write_memory", 
                                                                                                                                                                                                                                                                                                                                 "write_function"))), class = "request"), handle = '<pointer: 0x7ffb26c10060>'), class = "response"), 
                   structure(list(url = "https://test.api.com/v4/23456/information", 
                                  status_code = 204L, headers = structure(list(`cache-control` = "no-cache", 
                                                                               pragma = "no-cache", expires = "-1", `strict-transport-security` = "max-age=31536000", 
                                                                               `x-content-type-options` = "nosniff", `x-xss-protection` = "1; mode=block", 
                                                                               date = "Thu, 02 Sep 2021 20:24:43 GMT", `x-cdn` = "Imperva", 
                                                                               `x-iinfo` = "7-62735819-62735821 SNNy RT(1630614282821 130) q(0 0 0 -1) r(1 1) U6"), class = c("insensitive", 
                                                                                                                                                                              "list")), all_headers = list(list(status = 204L, version = "HTTP/1.1", 
                                                                                                                                                                                                                headers = structure(list(`cache-control` = "no-cache", 
                                                                                                                                                                                                                                         pragma = "no-cache", expires = "-1", `strict-transport-security` = "max-age=31536000", 
                                                                                                                                                                                                                                         `x-content-type-options` = "nosniff", `x-xss-protection` = "1; mode=block", 
                                                                                                                                                                                                                                         date = "Thu, 02 Sep 2021 20:24:43 GMT", `x-cdn` = "Imperva", 
                                                                                                                                                                                                                                         `x-iinfo` = "7-62735819-62735821 SNNy RT(1630614282821 130) q(0 0 0 -1) r(1 1) U6"), class = c("insensitive", 
                                                                                                                                                                                                                                                                                                                                        "list")))), cookies = structure(list(domain = c("#HttpOnly_.secure", 
                                                                                                                                                                                                                                                                                                                                                                                        ".secure", ".secure", "#HttpOnly_api.secure"
                                                                                                                                                                                                                                                                                                                                        ), flag = c(TRUE, TRUE, TRUE, FALSE), path = c("/", "/", 
                                                                                                                                                                                                                                                                                                                                                                                       "/", "/"), secure = c(TRUE, TRUE, TRUE, FALSE), expiration = structure(c(1661415232, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                Inf, Inf, Inf), class = c("POSIXct", "POSIXt")), name = c("visid_incap_754428", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          "nlbi_754428", "incap_ses_1286_754428", "ASP.NET_SessionId"
                                                                                                                                                                                                                                                                                                                                                                                                                                                                ), value = c("kpT+BJeYSuGXS5d9taQHzJiYJmEAAAAAQUIPAAAAAAAyrXukfkBb9mpZHnGcr6zX", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                             "pprNEnQFZjMz5rnaoK07BAAAAADZLQdRn3tbElZnp5AzMYef", "uTPqYDW0LgRsKI+vbsrYEW0xMWEAAAAAkinWhXc+BLr4Y9HRlIvAtQ==", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                             "5ndj041zg1zda2us2ai3eape")), row.names = c(NA, -4L), class = "data.frame"), 
                                  content = raw(0), date = structure(1630614283, class = c("POSIXct", 
                                                                                           "POSIXt"), tzone = "GMT"), times = c(redirect = 0, namelookup = 3.8e-05, 
                                                                                                                                connect = 4e-05, pretransfer = 0.000118, starttransfer = 0, 
                                                                                                                                total = 0.072601), request = structure(list(method = "POST", 
                                                                                                                                                                            url = "https://test.api.com/v4/23456/information", 
                                                                                                                                                                            headers = c(Accept = "application/json", Authorization = "Basic KFOSJFBfoepsEOTPSDnrfjs395031==", 
                                                                                                                                                                                        `Content-Type` = "application/json"), fields = NULL, 
                                                                                                                                                                            options = list(useragent = "libcurl/7.64.1 r-curl/4.3.2 httr/1.4.2", 
                                                                                                                                                                                           post = TRUE, postfieldsize = 157L, postfields = as.raw(c(0x20
                                                                                                                                                                                                                                                    ))), auth_token = NULL, 
                                                                                                                                                                            output = structure(list(), class = c("write_memory", 
                                                                                                                                                                                                                 "write_function"))), class = "request"), handle = '<pointer: 0x7ffb26c10060>'), class = "response"))

最佳答案

正如您提到的,您可以为一个 burb 运行 response_blurb$url ,但对于 blurb 列表,您必须执行 response_blurb[[1]]$url

可以对其进行扩展以捕获您想要的三个变量 response_blurb[[1]][c('url', 'date', 'status_code')]。这可以使用 sapply 迭代 [[1]][[2]]

把它们放在一起

sapply(response_blurb, '[', c('url', 'date', 'status_code'))

或返回数据框

data.frame(t(sapply(response_blurb, '[', c('url', 'date', 'status_code'))))

#-----
                                        url       date status_code
1 https://test.api.com/v4/12345/information 1630614283         204
2 https://test.api.com/v4/23456/information 1630614283         204

关于r - 从 API 响应列表中提取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69037017/

相关文章:

r - R中的模块化编程

performance - 分析调用其他函数的函数的性能

r - 如何防止 R 函数 "step"输出到控制台?

r - httr::POST 中查询和正文之间的区别

xml - 在 R 中抓取网页时出现问题

r - 使用 httr 为 Trello 创建 oauth token 时如何修复 "invalid return_url"错误?

xml - 使用 r httr 从谷歌搜索中抓取 url

javascript - 标题中的 R Shiny 动态文本(响应式(Reactive) renderUI)

r - 如何记录 R6 类的 S3 泛型?

r - 使用 R 接受 cookie 以下载 PDF 文件