r语言 - 啃食递归迪斯科列表



使用迪斯科,我获得了给定爵士音乐家的发行列表,如下所示:

releases <- list()
artists <- list()
artistURL <- "https://api.discogs.com/artists/"
library(jsonlite)
a <- function(artistcode){
for(i in 0:3){
artistset <- fromJSON(paste0(artistURL, artistcode, "/releases?page=", i))
message("Retrieving page ", i)
releases[[i+1]] <- (as.data.frame(artistset$releases.main_release))
}
return(artistset)
message("Total rows=", dim(artistset[[2]])[1] )
}
temp<-a('265634') # art tatum 265634
temp$releases$title # shows first 50 albums...where's the rest?

检查后,您将看到temp是两个列表,第二个称为版本。发行了50张专辑。但是,我在fromJSON命令中要求三页输出,但我temp有 22 页结果:

str(temp$pagination)  # there are 22 pages of 50 lines per page

如何将该艺术家的所有标题和其他数据(22 页(提取到数据帧中?一直在捣乱purrr无济于事。 感谢您的任何帮助!

这应该效果更好。releases仅在函数范围内定义,并未返回到全局环境。还更改了函数以使用 JSON 中的 pages 变量来构造循环:

a <- function(artistcode){
releases <- list()
metadata <- fromJSON(paste0(artistURL, artistcode, "/releases?page=", 1))
for (i in 1:metadata$`pagination`$pages){
message("Retrieving page ", i)
Sys.sleep(2) #added as I was being rate limited
releases[[i]] <- fromJSON(paste0(artistURL, artistcode, "/releases?page=", i))$releases
}
return(releases)
}
temp<-a('265634') # art tatum 265634
temp[[1]] # page 1
temp[[2]] # page 2

相关内容

  • 没有找到相关文章

最新更新