首页 > 解决方案 > R:无法读取 API 调用 - open.connection 中的错误(con,“rb”)

问题描述

我可以在 webbrowser 中看到 API 调用的内容,但是使用jsonlitepackage:得到这个错误read_json

Error in open.connection(con, "rb") : connection cannot be opened
Añso: Warning message:
In open.connection(con, "rb") :
  cannot open URL 'https://www.plazavea.com.pe/api/catalog_system/pub/products/search?fq=C:/678/687/&_from=21&_to=41&O=OrderByScoreDESC&': HTTP status was '206 Partial Content'

代码: :

library(rvest)
library(tidyverse)
library(jsonlite)



api_request <- "https://www.plazavea.com.pe/api/catalog_system/pub/products/search?fq=C:/678/687/&_from=21&_to=41&O=OrderByScoreDESC&"

product_data <- jsonlite::read_json(api_request)

标签: rjsonjsonlite

解决方案


使用 httr 然后提取as = 'text'并传递给parse_json(),或者简单地在响应对象as = 'parsed'的调用中指定。content()

library(httr)

api_request <- "https://www.plazavea.com.pe/api/catalog_system/pub/products/search?fq=C:/678/687/&_from=21&_to=41&O=OrderByScoreDESC&"

product_data <- content(httr::GET(api_request), as = 'parsed')

推荐阅读