首页 > 解决方案 > 从 API 响应列表中提取数据

问题描述

我从一个看起来像这样的 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>

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

网址 日期 地位
https://test.api.com/v4/12345/信息 21 年 9 月 2 日 20:24 204
https://test.api.com/v4/23456/信息 21 年 9 月 2 日 20:24 204

如果它只是一个响应,我可以通过做类似的事情来调用这个数据response_blurb$urlresponse_blurb$date但是有多个响应,我不能这样做。

输入如下:

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"))

标签: rhttr

解决方案


你可以试试这个:

library(purrr)
library(magrittr)

response_blurb %>% 
  map_df(~.x %>% 
           extract(c("url", "date", "status_code")))

返回

# A tibble: 2 x 3
  url                                       date                status_code
  <chr>                                     <dttm>                    <int>
1 https://test.api.com/v4/12345/information 2021-09-02 20:24:43         204
2 https://test.api.com/v4/23456/information 2021-09-02 20:24:43         204

编辑 感谢 nniloc 的评论,这可以缩短为

response_blurb %>% 
  map_df(extract, c("url", "date", "status_code"))

推荐阅读