All Articles

Weather API

1. 前言

平时做一些和生活、日常应用相关的功能时,偶尔会有些需要获取天气信息的功能。所以简单了解了点天气相关的API。之前在知乎上搜了下,找到一个topic:网上的天气 API 哪一个更加可靠?

不过总感觉国内的API一般都不咋的,所以兴趣不大,也没怎么尝试。下面简单介绍下尝试过的两个。

2. OpenWeather

国外推荐使用这个:OpenWeather。注册之后就会获得token / API Key,基本上调用API也只需要这个token。

价格:Pricing。免费版的账号限制:

60 calls/minute
1,000,000 calls/month

而且免费版的账号有一些API是无法调用的,比如说查询历史天气信息。

可供使用的API清单:Weather API。基本上和天气相关你想得到的,都能得到满足。下面会举几个常用的API例子,注意所有的API返回的温度都是开氏温度,需要提供如下参数才可以转为摄氏温度:

name mandatory description
units optional Units of measurement. standard, metric and imperial units are available. If you do not use the units parameter, standard units will be applied by default.

Temperature is available in Fahrenheit, Celsius and Kelvin units.

  • For temperature in Fahrenheit use units=imperial
  • For temperature in Celsius use units=metric
  • Temperature in Kelvin is used by default, no need to use units parameter in API call

Current Weather Data

$ curl "https://api.openweathermap.org/data/2.5/weather?q={city name}&appid={API key}"

$ curl "https://api.openweathermap.org/data/2.5/weather?q={city name},{state code}&appid={API key}"

$ curl "https://api.openweathermap.org/data/2.5/weather?q={city name},{state code},{country code}&appid={API key}"
{
  "coord": {
    "lon": -122.08,
    "lat": 37.39
  },
  "weather": [
    {
      "id": 800,
      "main": "Clear",
      "description": "clear sky",
      "icon": "01d"
    }
  ],
  "base": "stations",
  "main": {
    "temp": 282.55,
    "feels_like": 281.86,
    "temp_min": 280.37,
    "temp_max": 284.26,
    "pressure": 1023,
    "humidity": 100
  },
  "visibility": 16093,
  "wind": {
    "speed": 1.5,
    "deg": 350
  },
  "clouds": {
    "all": 1
  },
  "dt": 1560350645,
  "sys": {
    "type": 1,
    "id": 5122,
    "message": 0.0139,
    "country": "US",
    "sunrise": 1560343627,
    "sunset": 1560396563
  },
  "timezone": -25200,
  "id": 420006353,
  "name": "Mountain View",
  "cod": 200
}

Historical weather API

$ curl "https://history.openweathermap.org/data/2.5/history/city?q={city ID},{country code}&type=hour&start={start}&end={end}&appid={API key}"

$ curl "https://history.openweathermap.org/data/2.5/history/city?q={city ID},{country code}&type=hour&start={start}&cnt={cnt}&appid={API key}"
{
  "message": "",
  "cod": "200",
  "city_id": 2885679,
  "calctime": 0.0823,
  "cnt": 3,
  "list": [
    {
      "main": {
        "temp": 266.052,
        "temp_min": 266.052,
        "temp_max": 266.052,
        "pressure": 957.86,
        "sea_level": 1039.34,
        "grnd_level": 957.86,
        "humidity": 90
      },
      "wind": {
        "speed": 1.16,
        "deg": 139.502
      },
      "clouds": {
        "all": 0
      },
      "weather": [
        {
          "id": 800,
          "main": "Clear",
          "description": "Sky is Clear",
          "icon": "01n"
        }
      ],
      "dt": 1485722804
    },
    {
      "main": {
        "temp": 263.847,
        "temp_min": 263.847,
        "temp_max": 263.847,
        "pressure": 955.78,
        "sea_level": 1037.43,
        "grnd_level": 955.78,
        "humidity": 91
      },
      "wind": {
        "speed": 1.49,
        "deg": 159
      },
      "clouds": {
        "all": 0
      },
      "weather": [
        {
          "id": 800,
          "main": "Clear",
          "description": "Sky is Clear",
          "icon": "01n"
        }
      ],
      "dt": 1485749608
    },
    {
      "main": {
        "temp": 274.9,
        "pressure": 1019,
        "temp_min": 274.15,
        "temp_max": 275.15,
        "humidity": 88
      },
      "wind": {
        "speed": 1,
        "deg": 0
      },
      "clouds": {
        "all": 76
      },
      "weather": [
        {
          "id": 500,
          "main": "Rain",
          "description": "light rain",
          "icon": "10d"
        }
      ],
      "dt": 1485773778
    }
  ]
}

3. Nowapi

偶然搜到的这个API,国内公司。准确度暂时没有认真校对过,这个存疑,有需要用的需要认真看下。

这家公司做的API还相当多,全部列表在这里:Nowapi

天气API主要有下面这几个接口:

  • 天气预报(整合版)
  • 天气预报(5-7天)
  • 实时天气
  • 实时天气(批量)
  • 历史天气
  • PM2.5
  • AQI生活指数(5-7天)
  • 城市列表天气类型

注册账号之后在后台就可以看到自己的AppKeySign,这两个是用来调API的。

付费方面主要有两种形式:

  1. 按API调用频次分价格段的包月
  2. 按API实际使用次数购买次数

价格可以说相当亲民,轻度使用¥10买个15000次可以用很久了。

包月 nowapi time

买次数 nowapi count

选两个API看下:

实时天气

$ curl https://sapi.k780.com/?app=weather.today&weaId=1&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json (示例中sign会不定期调整)
{
  "success": "1",
  "result": {
    "weaid": "1",
    "days": "2014-07-30",
    "week": "星期三",
    "cityno": "beijing",
    "citynm": "北京",
    "cityid": "101010100",
    "temperature": "31℃/24℃",
    /*当日温度区间 (注: 夜间只有一个温度如24℃/24℃)*/
    "temperature_curr": "21℃",
    /*当前温度*/
    "humidity": "50%",
    /*湿度*/
    "aqi": "100",
    /*pm2.5 说明详见weather.pm25*/
    "weather": "多云转晴",
    /*天气*/
    "weather_icon": "http://api.k780.com/upload/weather/d/1.gif",
    /*气象图标 全部气象图标下载*/
    "weather_icon1": "",
    /*无意义不必理会*/
    "wind": "微风",
    /*风向*/
    "winp": "小于3级",
    /*风力*/
    "temp_high": "31",
    /*最高温度*/
    "temp_low": "24",
    /*最低温度*/
    "humi_high": "87.8",
    /*最大湿度 [历史遗留栏位不再更新]*/
    "humi_low": "75.2",
    /*最小湿度 [历史遗留栏位不再更新]*/
    "weatid": "2",
    /*天气ID,可对照weather.wtype接口中weaid*/
    "weatid1": "",
    /*无意义不必理会*/
    "windid": "1",
    /*风向ID(暂无对照表)*/
    "winpid": "2"
    /*风力ID(暂无对照表)*/
    "weather_iconid": "1"
    /*气象图标编号,对应weather_icon 1.gif*/
  }
}

历史天气

$ curl https://sapi.k780.com/?app=weather.history&weaId=1&date=2015-07-20&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json
{
  "success": "1",
  "result": [
    {
      "weaid": "1",
      "week": "星期一",
      "cityno": "beijing",
      "citynm": "北京",
      "cityid": "101010100",
      /*气象编号*/
      "uptime": "2015-07-20 00:50:00",
      /*更新时间*/
      "temperature": "22℃",
      /*该时段温度*/
      "humidity": "97%",
      /*该时段湿度*/
      "aqi": "101",
      /*PM2.5 AQI*/
      "weather": "晴",
      /*该时段天气*/
      "weather_icon": "http://api.k780.com/upload/weather/d/0.gif",
      /*时段气象图标 全部气象图标下载*/
      "wind": "东北风",
      /*该时段风向*/
      "winp": "1级",
      /*该时段风力*/
      "temp": "22",
      /*温度*/
      "weatid": "1",
      /*天气编号*/
      "windid": "13",
      /*风向编号*/
      "winpid": "201"
      /*风力编号*/
      "weather_iconid": "1"
      /*气象图标编号,对应weather_icon 0.gif*/
    },
    {
      "weaid": "1",
      "week": "星期一",
      "cityno": "beijing",
      "citynm": "北京",
      "cityid": "101010100",
      "uptime": "2015-07-20 01:50:00",
      "temperature": "22℃",
      "humidity": "99%",
      "aqi": "101",
      "weather": "晴",
      "weather_icon": "http://api.k780.com/upload/weather/d/0.gif",
      "wind": "东北风",
      "winp": "1级",
      "temp": "22",
      "weatid": "1",
      "windid": "13",
      "winpid": "201"
    }
  ]
}

EOF