1. 程式人生 > >天氣預報、查詢API介面大全(最新)

天氣預報、查詢API介面大全(最新)

  自序:

       由放飛夢想的專欄收集整理,來源於網路,基本收集了網路上能使用的大部分天氣API介面,本人水平有限,難免有遺漏或錯誤的地方,歡迎反饋

 前言:

    暫無

目錄:

  1.  中國天氣網

     1.1  實時天氣 API 介面

     1.2  當天天氣 API介面

     1.3 生活指數 API介面

     1.4 未來五天天氣 API 介面(失效)

 2.   開放資料網天氣

      2.1 簡介

      2.2. 一天天氣 API 介面

      2.3 未來五天天氣 API 介面

3.   中國天氣&開放資料 綜合版

    3.1  實時天氣

    3.2  當天天氣

    3.3 未來五天

    3.4 生活指數

4.  隨身雲天氣API (無天氣標誌)

    4.1 簡介

    4.1  簡化版 未來6天天氣 API介面

    4.2   詳細版 未來6天 天氣API介面

5. 新浪天氣

    5. 1  簡介

    5.2   使用方法

    5.3   天氣標誌

6.  總結

正文:

第一節: 中國天氣網

    1.1 簡介

       中國最權威的天氣預報網路平臺,很多平臺實際上也是用的他們的資料。

        官方網站:http://www.weather.com.cn

     1.2  地方程式碼獲取方法

         它對外提供了有限的免費API介面,通過地方程式碼來查詢,使用前先需要確定地方程式碼:

         在搜尋框上輸入你要需要獲得天氣的城市,點選查詢,即可在位址列獲得相應城市編號,示範:北京的地方程式碼:101010100

     1.3  實時天氣介面

     返回格式:

    {"weatherinfo":{"city":"北京","cityid":"101010100","temp":"23","WD":"東風","WS":"2級","SD":"92%","WSE":"2","time":"09:55","isRadar":"1","Radar":"JC_RADAR_AZ9010_JB"}}

      說明:其中 city 是地方名稱,cityid是地方程式碼,temp是當前溫度,"WD"是風向,“WS"是風速”,“SD"是溼度,time是更新時間。

    1.4 獲取當天天氣:

   返回格式:

      weatherinfo":{"city":"北京","cityid":"101010100","temp1":"29℃","temp2":"24℃","weather":"陰轉晴","img1":"d2.gif","img2":"n0.gif","ptime":"08:00"}}

       注意:其中 img1和img2是天氣標誌圖片,介面格式如下:

          這個圖就是天氣現象0(晴)的圖片,其他天氣現象的圖片依此類推。c打頭的圖片是20*20畫素的,b打頭的是50*46畫素的,d打頭的是反白的圖示,  29*20畫素,n打頭的是夜間反白圖示,29*20畫素,注意這裡的檔名是兩位數字!

  1.5  生活指數:

      返回格式: 文字較多,這裡省略,請點選訪問。

   1.6 獲取未來幾天天氣

     這個是獲取未來5天天氣的介面,可惜已經失效,不能獲取到最新的準確資料。

      解決方法是可以採集主站資料,缺點是一旦主頁佈局修改,就會失效,比如 2014年7月29號網站就改版過一次。

2. 開放資料 - k780資料

    2.1 簡介

       說明: 資料來自國家氣象局天氣網,每小時更新一次    

       免費版有每小時點選次數的限制(免費版最多每小時72000次查詢)

     2.2   獲取今日天氣

返回例項:

 <?xml version="1.0" encoding="utf-8" ?>

- <root>  <success>1</success> - <result>  <weaid>1</weaid>  <days>2014-07-30</days>  <week>星期三</week>  <cityno>beijing</cityno>  <citynm>北京</citynm>  <cityid>101010100</cityid>  <temperature>29℃/24℃</temperature>  <humidity>84.2℉/75.2℉</humidity>  <weather>陰轉晴</weather>  <weather_icon>http://api.k780.com:88/upload/weather/d/2.gif</weather_icon>  <weather_icon1>http://api.k780.com:88/upload/weather/d/0.gif</weather_icon1>  <wind>微風</wind>  <winp>小於3級</winp>  <temp_high>29</temp_high>  <temp_low>24</temp_low>  <humi_high>84.2</humi_high>  <humi_low>75.2</humi_low>  <weatid>3</weatid>  <weatid1>1</weatid1>  <windid>1</windid>  <winpid>2</winpid>  </result>  </root>

         引數說明:其中 ,wedid 是城市氣象編號,與中國天氣網編號一致,key和sign是身份驗證,註冊一個可以換成自己的,上例中的10003是公眾帳號,formt是返回格式,支援 json,xml,除了formt不是必須的以外,其他引數都是必須提供的。

  比如 我想獲得深圳的今日資料:

  第一步: 首先查詢深圳的城市程式碼

   經查詢,深圳的地方程式碼是 101280601

 第二步: 通過地方程式碼獲取今日天氣

          深圳的今天天氣資料就是:

  2.3  獲取未來五天

      請求示例(Xml)

 返回示例:

 <?xml version="1.0" encoding="utf-8" ?>

- <root>  <success>1</success> - <result> - <item_0>  <weaid>1</weaid>  <days>2014-07-30</days>  <week>星期三</week>  <cityno>beijing</cityno>  <citynm>北京</citynm>  <cityid>101010100</cityid>  <temperature>29℃/24℃</temperature>  <humidity>84.2℉/75.2℉</humidity>  <weather>陰轉晴</weather>  <weather_icon>http://api.k780.com:88/upload/weather/d/2.gif</weather_icon>  <weather_icon1>http://api.k780.com:88/upload/weather/d/0.gif</weather_icon1>  <wind>微風</wind>  <winp>小於3級</winp>  <temp_high>29</temp_high>  <temp_low>24</temp_low>  <humi_high>84.2</humi_high>  <humi_low>75.2</humi_low>  <weatid>3</weatid>  <weatid1>1</weatid1>  <windid>1</windid>  <winpid>2</winpid>  </item_0> - <item_1>  <weaid>1</weaid>  <days>2014-07-31</days>  <week>星期四</week>  <cityno>beijing</cityno>  <citynm>北京</citynm>  <cityid>101010100</cityid>  <temperature>31℃/24℃</temperature>  <humidity>87.8℉/75.2℉</humidity>  <weather></weather>  <weather_icon>http://api.k780.com:88/upload/weather/d/0.gif</weather_icon>  <weather_icon1>http://api.k780.com:88/upload/weather/d/0.gif</weather_icon1>  <wind>微風</wind>  <winp>小於3級</winp>  <temp_high>31</temp_high>  <temp_low>24</temp_low>  <humi_high>87.8</humi_high>  <humi_low>75.2</humi_low>  <weatid>1</weatid>  <weatid1>1</weatid1>  <windid>1</windid>  <winpid>2</winpid>  </item_1> - <item_2>  <weaid>1</weaid>  <days>2014-08-01</days>  <week>星期五</week>  <cityno>beijing</cityno>  <citynm>北京</citynm>  <cityid>101010100</cityid>  <temperature>33℃/25℃</temperature>  <humidity>91.4℉/77℉</humidity>  <weather></weather>  <weather_icon>http://api.k780.com:88/upload/weather/d/0.gif</weather_icon>  <weather_icon1>http://api.k780.com:88/upload/weather/d/0.gif</weather_icon1>  <wind>微風</wind>  <winp>小於3級</winp>  <temp_high>33</temp_high>  <temp_low>25</temp_low>  <humi_high>91.4</humi_high>  <humi_low>77</humi_low>  <weatid>1</weatid>  <weatid1>1</weatid1>  <windid>1</windid>  <winpid>2</winpid>  </item_2> - <item_3>  <weaid>1</weaid>  <days>2014-08-02</days>  <week>星期六</week>  <cityno>beijing</cityno>  <citynm>北京</citynm>  <cityid>101010100</cityid>  <temperature>34℃/24℃</temperature>  <humidity>93.2℉/75.2℉</humidity>  <weather></weather>  <weather_icon>http://api.k780.com:88/upload/weather/d/0.gif</weather_icon>  <weather_icon1>http://api.k780.com:88/upload/weather/d/0.gif</weather_icon1>  <wind>微風</wind>  <winp>小於3級</winp>  <temp_high>34</temp_high>  <temp_low>24</temp_low>  <humi_high>93.2</humi_high>  <humi_low>75.2</humi_low>  <weatid>1</weatid>  <weatid1>1</weatid1>  <windid>1</windid>  <winpid>2</winpid>  </item_3> - <item_4>  <weaid>1</weaid>  <days>2014-08-03</days>  <week>星期日</week>  <cityno>beijing</cityno>  <citynm>北京</citynm>  <cityid>101010100</cityid>  <temperature>33℃/25℃</temperature>  <humidity>91.4℉/77℉</humidity>  <weather>晴轉多雲</weather>  <weather_icon>http://api.k780.com:88/upload/weather/d/0.gif</weather_icon>  <weather_icon1>http://api.k780.com:88/upload/weather/d/1.gif</weather_icon1>  <wind>微風</wind>  <winp>小於3級</winp>  <temp_high>33</temp_high>  <temp_low>25</temp_low>  <humi_high>91.4</humi_high>  <humi_low>77</humi_low>  <weatid>1</weatid>  <weatid1>2</weatid1>  <windid>1</windid>  <winpid>2</winpid>  </item_4> - <item_5>  <weaid>1</weaid>  <days>2014-08-04</days>  <week>星期一</week>  <cityno>beijing</cityno>  <citynm>北京</citynm>  <cityid>101010100</cityid>  <temperature>33℃/25℃</temperature>  <humidity>91.4℉/77℉</humidity>  <weather>多雲</weather>  <weather_icon>http://api.k780.com:88/upload/weather/d/1.gif</weather_icon>  <weather_icon1>http://api.k780.com:88/upload/weather/d/1.gif</weather_icon1>  <wind>微風</wind>  <winp>小於3級</winp>  <temp_high>33</temp_high>  <temp_low>25</temp_low>  <humi_high>91.4</humi_high>  <humi_low>77</humi_low>  <weatid>2</weatid>  <weatid1>2</weatid1>  <windid>1</windid>  <winpid>2</winpid>  </item_5>  </result>  </root>

   轉換方法與前面類似,這裡就不需要重複了。

3. 綜合方案

    3. 1 實時天氣

    3.2  今天天氣

         2個選擇: 

     3.3  未來幾天天氣

           今天天氣及未來五天天氣 :

    3.4    生活指數

 4. 隨身雲 天氣API介面

    4.1 簡介
        這個是官方沒有公開的介面,是網路上有人訪問中國農曆網無意中擷取到的,所以不保證穩定性,還有一個缺點是不能獲取天氣標誌圖片。
     4.2  簡化版介面
      引數citykey就是設定地方程式碼,和中國天氣網的地方程式碼一樣,替換要查詢的地方程式碼即可,下同。
返回示例:

{"desc":"OK","status":1000,"data":{"wendu":"28","ganmao":"相對今天出現了較大幅度降溫,較易發生感冒,體質較弱的朋友請注意適當防護。","forecast":[{"fengxiang":"微風","fengli":"小於3級","high":"高溫 29℃","type":"陰","low":"低溫 24℃","date":"30日星期三"},{"fengxiang":"微風","fengli":"小於3級","high":"高溫 31℃","type":"晴","low":"低溫 24℃","date":"31日星期四"},{"fengxiang":"微風","fengli":"小於3級","high":"高溫 33℃","type":"晴","low":"低溫 25℃","date":"1日星期五"},{"fengxiang":"微風","fengli":"小於3級","high":"高溫 34℃","type":"晴","low":"低溫 24℃","date":"2日星期六"},{"fengxiang":"微風","fengli":"小於3級","high":"高溫 33℃","type":"晴","low":"低溫 25℃","date":"3日星期天"},{"fengxiang":"微風","fengli":"小於3級","high":"高溫 33℃","type":"多雲","low":"低溫 25℃","date":"4日星期一"},{"fengxiang":"微風","fengli":"小於3級","high":"高溫 33℃","type":"多雲","low":"低溫 25℃","date":"5日星期二"}],"yesterday":{"fl":"小於3級","fx":"微風","high":"高溫 29℃","type":"中雨","low":"低溫 22℃","date":"29日星期二"},"aqi":"186","city":"北京"}}

    2.  詳細版介面
返回示例:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. - <resp>
  3. <city>北京</city>
  4. <updatetime>16:05</updatetime>
  5. <wendu>28</wendu>
  6. <fengli>2級</fengli>
  7. <shidu>66%</shidu>
  8. <fengxiang>東風</fengxiang>
  9. <sunrise_1>05:10</sunrise_1>
  10. <sunset_1>19:31</sunset_1>
  11. <sunrise_2/>
  12. <sunset_2/>
  13. - <environment>
  14. <aqi>186</aqi>
  15. <pm25>140</pm25>
  16. <suggest>...適量減少戶外運動</suggest>
  17. <quality>中度汙染</quality>
  18. <MajorPollutants>顆粒物(PM2.5)</MajorPollutants>
  19. <o3>142</o3>
  20. <co>2</co>
  21. <pm10>0</pm10>
  22. <so2>4</so2>
  23. <no2>24</no2>
  24. <time>15:00:00</time>
  25. </environment>
  26. - <yesterday>
  27. <date_1>29日星期二</date_1>
  28. <high_1>高溫 29℃</high_1>
  29. <low_1>低溫 22℃</low_1>
  30. - <day_1>
  31. <type_1>中雨</type_1>
  32. <fx_1>微風</fx_1>
  33. <fl_1>小於3級</fl_1>
  34. </day_1>
  35. - <night_1>
  36. <type_1>中雨</type_1>
  37. <fx_1>微風</fx_1>
  38. <fl_1>小於3級</fl_1>
  39. </night_1>
  40. </yesterday>
  41. - <forecast>
  42. - <weather>
  43. <date>30日星期三</date>
  44. <high>高溫 29℃</high>
  45. <low>低溫 24℃</low>
  46. - <day>
  47. <type></type>
  48. <fengxiang>微風</fengxiang>
  49. <fengli>小於3級</fengli>
  50. </day>
  51. - <night>
  52. <type></type>
  53. <fengxiang>微風</fengxiang>
  54. <fengli>小於3級</fengli>
  55. </night>
  56. </weather>
  57. - <weather>
  58. <date>31日星期四</date>
  59. <high>高溫 31℃</high>
  60. <low>低溫 24℃</low>
  61. - <day>
  62. <type></type>
  63. <fengxiang>微風</fengxiang>
  64. <fengli>小於3級</fengli>
  65. </day>
  66. - <night>
  67. <type></type>
  68. <fengxiang>微風</fengxiang>
  69. <fengli>小於3級</fengli>
  70. </night>
  71. </weather>
  72. - <weather>
  73. <date>1日星期五</date>
  74. <high>高溫 33℃</high>
  75. <low>低溫 25℃</low>
  76. - <day>
  77. <type></type>
  78. <fengxiang>微風</fengxiang>
  79. <fengli>小於3級</fengli>
  80. </day>
  81. - <night>
  82. <type></type>
  83. <fengxiang>微風</fengxiang>
  84. <fengli>小於3級</fengli>
  85. </night>
  86. </weather>
  87. - <weather>
  88. <date>2日星期六</date>
  89. <high>高溫 34℃</high>
  90. <low>低溫 24℃</low>
  91. - <day>
  92. <type></type>
  93. <fengxiang>微風</fengxiang>
  94. <fengli>小於3級</fengli>
  95. </day>
  96. - <night>
  97. <type></type>
  98. <fengxiang>微風</fengxiang>
  99. <fengli>小於3級</fengli>
  100. </night>
  101. </weather>
  102. - <weather>
  103. <date>3日星期天</date>
  104. <high>高溫 33℃</high>
  105. <low>低溫 25℃</low>
  106. - <day>
  107. <type>&