reslist$subjects[[1]] 中的 R 错误:下标越界
R Error in reslist$subjects[[1]] : subscript out of bounds
想获取一些电影的评分,不知道如何解决"subscript out of bounds"的问题。这是我的代码。
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) 您使用了错误的端点。
2) 您构造的 URL 字符串不正确。通过将其复制粘贴到浏览器中查看它的输出:https://api.douban.com/v2/movie/search?q={Life%20Is%20Beautiful}
正确的端点是:
获取 /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\" 的结果"
想获取一些电影的评分,不知道如何解决"subscript out of bounds"的问题。这是我的代码。
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) 您使用了错误的端点。
2) 您构造的 URL 字符串不正确。通过将其复制粘贴到浏览器中查看它的输出:https://api.douban.com/v2/movie/search?q={Life%20Is%20Beautiful}
正确的端点是: 获取 /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\" 的结果"