【问题标题】:API Query for loopAPI 查询循环
【发布时间】:2017-07-22 06:42:00
【问题描述】:

我正在尝试从 API 中提取一些数据,并将其全部放入单个数据框中。我试图将一个变量放入我从中提取的 URL 中,然后循环它以从 54 个键中提取数据。这是我到目前为止的笔记。

library("jsonlite")
library("httr")
library("lubridate")
options(stringsAsFactors = FALSE)
url <- "http://api.kuroganehammer.com"

### This gets me a list of 58 observations, I want to use this list to 
### pull data for each using an API

raw.characters <- GET(url = url, path = "api/characters")
## Convert the results from unicode to a JSON
text.raw.characters <- rawToChar(raw.characters$content)
## Convert the JSON into an R object. Check the class of the object after 
## it's retrieved and reformat appropriately
characters <- fromJSON(text.raw.characters)
class(characters)

## This pulls data for an individual character. I want to get one of 
## these for all 58 characters by looping this and replacing the 1 in the
## URL path for every number through 58.
raw.bayonetta <- GET(url = url, path = "api/characters/1/detailedmoves")
text.raw.bayonetta <- rawToChar(raw.bayonetta$content)
bayonetta <- fromJSON(text.raw.bayonetta)

## This is the function I tried to create, but I get a lexical error when 
## I call it, and I have no idea how to loop it.
move.pull <- function(x) {
  char.x <- x
  raw.x <- GET(url = url, path = cat("api/characters/",char.x,"/detailedmoves", sep = ""))
  text.raw.x <- rawToChar(raw.x$content)
  char.moves.x <- fromJSON(text.raw.x)
  char.moves.x$id <- x
  return(char.moves.x)
}

【问题讨论】:

    标签: r api loops


    【解决方案1】:

    本文的第一部分:

    library(jsonlite)
    library(httr)
    library(lubridate)
    library(tidyverse)
    
    base_url <- "http://api.kuroganehammer.com"
    
    res <- GET(url = base_url, path = "api/characters")
    content(res, as="text", encoding="UTF-8") %>% 
      fromJSON(flatten=TRUE) %>% 
      as_tibble() -> chars
    

    获取字符的数据框。

    这个:

    pb <- progress_estimated(length(chars$id))
    map_df(chars$id, ~{
    
      pb$tick()$print()
    
      Sys.sleep(sample(seq(0.5, 2.5, 0.5), 1)) # be kind to the free API
    
      res <- GET(url = base_url, path = sprintf("api/characters/%s/detailedmoves", .x))
    
      content(res, as="text", encoding="UTF-8") %>% 
        fromJSON(flatten=TRUE) %>% 
        as_tibble() 
    
    }, .id = "id") -> moves
    

    为您获取所有“动作”的数据框并为角色添加“id”。您还可以免费获得一个进度条。

    然后,您可以根据需要 left_join() 或将移动数据分组并嵌套到单独的列表嵌套列中。如果你想开始,你可以使用map() vs map_df()

    留下时间暂停代码。这是一个免费的 API,您应该增加暂停时间以避免 DoS 攻击他们的网站。

    【讨论】:

    • 这太棒了,效果很好!我还加载了 dplyr 和 purrr 以使管道运算符和 map_df 工作。感谢有关时间暂停代码的提示,我从来没有想过这样做!
    • tidyverse 让你 dplyr & purrr
    • 啊,我明白了,我想我的图书馆里没有它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多