代码之家  ›  专栏  ›  技术社区  ›  Anakin Skywalker

如何通过R访问空气质量API?

  •  1
  • Anakin Skywalker  · 技术社区  · 7 年前

    AirQuality API ,非常新,没有看到简单的R教程。

    我有兴趣得到洛杉矶的数据作为样本

    api.airvisual.com/v2/city?city=Los Angeles&state=California&country=USA&key={{KEY}}
    

    我的证件

    (他们就是这么说的)

    密码<-“密钥”

    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)中出错{:参数的长度为零

    它们的参数是

    参数

    城市:

    国家:

    国家的英文名称,可以使用相应的列表端点找到。

    当我使用

    url <- "api.airvisual.com/v2/city?city=Los%20Angeles&state=California&country=USA&key={{KEY}}"
    

    如果是%20而不是空格,它会给我一个错误

    0 回复  |  直到 7 年前
        1
  •  2
  •   makeshift-programmer    7 年前

    我不认为您需要指定端点。您可以直接使用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"
    

    让我知道这是否有效。

    P、 我甚至试过在洛杉矶用%20来代替空格,这很管用。

        2
  •  1
  •   Hong Ooi    7 年前

    library(httr)
    GET("https://api.airvisual.com/....")
         ^^^^^^^^