-1

I want to acquire the ranking score of some movies, and not knowing how to fix the problem with "subscript out of bounds". Here is my code.

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") 

And the error is like :

Error in reslist$subjects[[1]] : subscript out of bounds
victordongy
  • 325
  • 2
  • 4
  • 13

1 Answers1

0

1) You are using the wrong Endpoint.

2) The URL String you are constructing is not correct . Look at it's output by copy-pasting this into browser: https://api.douban.com/v2/movie/search?q={Life%20Is%20Beautiful}

The correct Endpoint is: GET /v2/movie/subject/1764796

If you want to use the same endpoint as you have and get its contents, do this:

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\" 的结果"
vagabond
  • 3,526
  • 5
  • 43
  • 76