R 如何从多个json页面的多个列表中提取值

R 如何从多个json页面的多个列表中提取值,r,R,我试图从几个页面中提取来自json内容的电影的“标题”和“id”。这是我用于发出请求的代码: themoviedb_ip_address <- "https://api.themoviedb.org" themovidedb_discover_movie_url <- "/3/discover/movie" pages <- c(1,2) themovidedb_discover_movie_req <- paste(themoviedb_ip_address,

我试图从几个页面中提取来自json内容的电影的“标题”和“id”。这是我用于发出请求的代码:

themoviedb_ip_address <- "https://api.themoviedb.org"
themovidedb_discover_movie_url <- "/3/discover/movie"

pages <- c(1,2)

themovidedb_discover_movie_req <- paste(themoviedb_ip_address,
                                        themovidedb_discover_movie_url,
                                        "?",
                                        api_key,"&sort_by=revenue.desc",
                                        "&include_adult=false",
                                        "&include_video=false",
                                        "&page=",
                                        "{pages}",
                                        "&primary_release_year=2010",
                                        "&with_genres=18",
                                        sep = "")

movie_revenue_2010 <- str_glue(themovidedb_discover_movie_req) %>%
  map(GET) %>%
  map(content,as = "parsed") %>%
  map(purrr::pluck, "results")
移动的B\u ip\u地址%
映射(purrr::pull,“结果”)%>%
地图(magrittr::摘录,c(“标题”,“id”))
我得到以下错误:错误:参数1必须有名称

请注意,以下代码工作正常:

for(i in 1:2){

  themovidedb_discover_movie_query_string <- paste("&sort_by=revenue.desc",
                                                   "&include_adult=false",
                                                   "&include_video=false",
                                                   "&page=",
                                                   i,
                                                   "&primary_release_year=2010",
                                                   "&with_genres=18",
                                                   sep = "")

  #print(themovidedb_discover_movie_query_string)

  movie_revenue_2010_req <-
    httr::GET(paste(themoviedb_ip_address,
                    themovidedb_discover_movie_url,
                    "?",
                    api_key,
                    themovidedb_discover_movie_query_string,
                    sep = ""))

  movie_revenue_2010_content <- httr::content(movie_revenue_2010_req,
                                              as = "parsed")

  movie_revenue_2010 <- purrr::pluck(movie_revenue_2010_content, "results")
  movie_revenue_2010_tbl <- movie_revenue_2010_tbl %>%
    bind_rows(map_df(movie_revenue_2010, extract, c("title", "id")))
}
for(1:2中的i){

movideb_discover_movie_query_string假设您的嵌套列表名为
temp
,我们可以这样做

library(purrr)
map(temp, ~map_df(.x, `[`, c('title', 'id')))

#[[1]]
# A tibble: 20 x 2
#   title                              id
#   <chr>                           <int>
# 1 The Twilight Saga: Eclipse      24021
# 2 The King's Speech               45269
# 3 The Karate Kid                  38575
# 4 Black Swan                      44214
# 5 Robin Hood                      20662
#...
#...

#[[2]]
# A tibble: 20 x 2
#   title                                                 id
#   <chr>                                              <int>
# 1 The Other Woman                                    52505
# 2 Green Zone                                         22972
# 3 The Fighter                                        45317
# 4 Burlesque                                          42297
# 5 Letters to Juliet                                  37056
#...
lapply(temp, function(x) do.call(rbind, lapply(x, `[`, c('title', 'id'))))


分别用于与上述相同的输出。

您可以使用
dput
共享数据的可复制示例吗?我用dput()内容更新了描述。您可以检查是否运行此部分
map\u df(magrittr::extract,c(“title”,“id”))
使用
map
生成结果?如果是,则在导致此错误的某些结果中生成了
NA
。使用map而不是map\u df会得到以下结果:list(list(NULL,NULL),list(NULL,NULL))
library(purrr)
map(temp, ~map_df(.x, `[`, c('title', 'id')))

#[[1]]
# A tibble: 20 x 2
#   title                              id
#   <chr>                           <int>
# 1 The Twilight Saga: Eclipse      24021
# 2 The King's Speech               45269
# 3 The Karate Kid                  38575
# 4 Black Swan                      44214
# 5 Robin Hood                      20662
#...
#...

#[[2]]
# A tibble: 20 x 2
#   title                                                 id
#   <chr>                                              <int>
# 1 The Other Woman                                    52505
# 2 Green Zone                                         22972
# 3 The Fighter                                        45317
# 4 Burlesque                                          42297
# 5 Letters to Juliet                                  37056
#...
map_df(temp, ~map_df(.x, `[`, c('title', 'id')))

# A tibble: 40 x 2
#   title                         id
#   <chr>                      <int>
# 1 The Twilight Saga: Eclipse 24021
# 2 The King's Speech          45269
# 3 The Karate Kid             38575
# 4 Black Swan                 44214
# 5 Robin Hood                 20662
# 6 Shutter Island             11324
# 7 Sex and the City 2         37786
# 8 True Grit                  44264
# 9 The Social Network         37799
#10 The Sorcerer's Apprentice  27022
# … with 30 more rows
lapply(temp, function(x) do.call(rbind, lapply(x, `[`, c('title', 'id'))))
do.call(rbind, lapply(temp, function(x) do.call(rbind,lapply(x, `[`, c('title', 'id')))))