尝试访问AirQuality API,这是非常新的,没有看到简单的R教程。
得到了我的用户名和密码。
我有兴趣获得LA数据作为样本
api.airvisual.com/v2/city?city=Los Angeles&state=California&country=USA&key={{KEY}}我的证件
用户名<- -“我的新免费api密钥”(这就是他们给它的方式)
密码<-“密钥”
然后我尝试使用教程中的一些代码。
library(httr)
library(jsonlite)
url <- "api.airvisual.com/v2/city?city=Los Angeles&state=California&country=USA&key={{KEY}}"
endpoint <- "city"
some_data <- GET(url, endpoint)它给了我一个错误
if (is_http) {:参数长度为零的中的错误
它们的参数是
参数
城市:城市的英文名称,可以使用相应的列表端点找到。
状态:状态的英文名称,可以使用相应的列表端点找到。
国家:国家的英文名称,可以使用相应的列表端点找到。
我确信这是一件非常简单的事情,但我从来没有这样做过,所以我绊倒了。请给我建议。
UPD.
当我用
url <- "api.airvisual.com/v2/city?city=Los%20Angeles&state=California&country=USA&key={{KEY}}"如果使用%20而不是空格,它会给我一个错误
Error in UseMethod("as.request"):没有适用于类“字符”对象的“as.request”方法
发布于 2019-10-23 07:34:04
我不认为您需要指定端点。可以直接使用GET,如下所示:
require(httr)
response = GET("https://api.airvisual.com/v2/city?city=Los%20Angeles&state=California&country=USA&key={{my_private_key}}")如果您还需要访问响应中的数据,代码将是:
data = content(response)data是一个列表,如下所示:
$status
[1] "success"
$data
$data$city
[1] "Los Angeles"
$data$state
[1] "California"
$data$country
[1] "USA"
$data$location
$data$location$type
[1] "Point"
$data$location$coordinates
$data$location$coordinates[[1]]
[1] -118.2417
$data$location$coordinates[[2]]
[1] 34.0669
$data$current
$data$current$weather
$data$current$weather$ts
[1] "2019-10-23T07:00:00.000Z"
$data$current$weather$tp
[1] 20
$data$current$weather$pr
[1] 1014
$data$current$weather$hu
[1] 52
$data$current$weather$ws
[1] 0.78
$data$current$weather$wd
[1] 345
$data$current$weather$ic
[1] "01n"
$data$current$pollution
$data$current$pollution$ts
[1] "2019-10-23T07:00:00.000Z"
$data$current$pollution$aqius
[1] 37
$data$current$pollution$mainus
[1] "p1"
$data$current$pollution$aqicn
[1] 41
$data$current$pollution$maincn
[1] "p1"如果这有用的话请告诉我。
我甚至试着让洛杉矶用%20代替空间,这样很有效。
发布于 2019-10-23 07:40:51
您必须将该方案作为URL的一部分:
library(httr)
GET("https://api.airvisual.com/....")
^^^^^^^^https://stackoverflow.com/questions/58515428
复制相似问题