API Query for loop

前端 未结 1 2064
孤城傲影
孤城傲影 2020-12-17 07:08

I\'m trying to pull some data from an API throw it all into a single data frame. I\'m trying to put a variable into the URL I\'m pulling from and then loop it to pull data f

1条回答
  •  旧巷少年郎
    2020-12-17 08:07

    The first part of this:

    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
    

    Gets you a data frame of the characters.

    This:

    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
    

    Gets you a data frame of all the "moves" and adds the "id" for the character. You get a progress bar for free, too.

    You can then either left_join() as needed or group & nest the moves data into a separate list-nest column. If you want that to begin with you can use map() vs map_df().

    Leave in the time pause code. It's a free API and you should likely increase the pause times to avoid DoS'ing their site.

    0 讨论(0)
提交回复
热议问题