R reslist $ subject [[1]]中的错误:下标超出范围

时间:2016-11-04 06:05:14

标签: r rcurl subscript

我想获得一些电影的排名分数,而不知道如何解决问题"下标超出范围"。这是我的代码。

movieScoreapi <- function(x) {
api <- "https://api.douban.com/v2/movie/search?q={"
url <- paste(api, x, "}", sep = "")    
res <- getURL(url)    
reslist <- fromJSON(res)    
name <- reslist$subjects[[1]]$title    
score <- reslist$subjects[[1]]$rating$average    
return(list(name = name, score = score))
}
movieScoreapi("Life is beautiful") 

错误就像:

Error in reslist$subjects[[1]] : subscript out of bounds

1 个答案:

答案 0 :(得分:0)

1)您使用的是错误的端点。

2)您构建的URL字符串不正确。通过将其复制粘贴到浏览器中来查看其输出:https://api.douban.com/v2/movie/search?q= {Life%20Is%20Beautiful}

正确的端点是: GET / v2 / movie / subject / 1764796

如果您想使用相同的端点并获取其内容,请执行以下操作:

install.packages("httr")
require(httr)

movieScoreapi <- function(x) {
api <- "https://api.douban.com/v2/movie/search?q="
  url <- paste(api, x, sep = "")    
  res <- GET(url)    
  reslist <- content(res)    
  df_contents <- rbind(unlist(content(res)))
  return(df_contents)
}
movieScoreapi("Dracula") 
     count start total title                            
[1,] "20"  "0"   "0"   "搜索 \"LifeIsBeautiful\" 的结果"