天氣預報接口

中國天氣網(weather.com

【該接口如果用js直接調用會出現跨域問題,可以以自己的服務器作爲跳板,js請求自己的服務器,自己的服務器(例如:PHP)再去請求該接口】

  • http://m.weather.com.cn/data/101110101.html(六天預報)
  • http://www.weather.com.cn/data/sk/101110101.html(實時天氣信息)
  • http://m.weather.com.cn/data5/city.xml(獲得第一級城市代碼【省級】)
其中101110101是城市的代碼,那麼我們如何得到城市的代碼呢?
首先調用http://m.weather.com.cn/data5/city.xml得到XML如下:
<parsererror>
<sourcetext>01| 北京,02|上海,03|天津,04|重慶,05|黑龍江,06|吉林,07|遼寧,08|內蒙古,09|河北,10|山西,11|陝西,12|山東,13|新疆,14|西藏,15|青海,16|甘肅,17|寧夏,18|河南,19|江蘇,20|湖北,21|浙江,22|安徽,23|福建,24|江西,25|湖南,26|貴州,27|四川,28|廣東,29|雲南,30|廣西,31|海南,32|香港,33|澳門,34|臺灣
</sourcetext></parsererror>
這部分是全國省份的List
現在例如我們要得到江蘇的城市的list則調用
http://m.weather.com.cn/data5/city19.xml(在city後加入省份的ID)得到XML如下:

<parsererror>
<sourcetext>1901|南京,1902|無錫,1903|鎮江,1904|蘇州,1905|南通,1906|揚州,1907|鹽城,1908|徐州,1909|淮安,1910|連雲港,1911|常州,1912|泰州,1913|宿遷 </sourcetext>
</parsererror>

然後我想得到常州市底下的區縣則調用
http://m.weather.com.cn/data5/city1911.xml(在city後加入市級的ID)得到XML如下:
<parsererror>
<sourcetext>191101|常州,191102|溧陽,191103|金壇,191104|武進 ^</sourcetext>
</parsererror>

然後我想得到金壇底下的地方名則調用
http://m.weather.com.cn/data5/city191103.xml(在city後加入市級的ID)得到XML如下:
<parsererror>
<sourcetext>191103|101191103</sourcetext>
</parsererror>
出現這種情況則說明你的地名已經到最後一級了此事你得到的 101191103則是城市的代碼
此時你就可以用
  • http://m.weather.com.cn/data/101191103.html(六天預報)
  • http://www.weather.com.cn/data/sk/101191103.html(實時天氣信息)

http://m.weather.com.cn/data/101191103.html(六天預報)得到的結果如下:

  1. ①weather1至weather6 是六天的天氣(包含當天)
  2. ②temp1至temp6是六天的溫度範圍
  3. ③image_title1至image_title12是六天的天氣(分爲白天和晚上 image_title1是今天白天 , image_title1是今天晚上 ,image_title3是明天白天,依次類推)
  4. ④img1至img12 是六天的天氣所對應的圖標(分爲白天和晚上,和 image_title 是對應的,例如:img1的值是7 ,則圖標對應的urlhttp://m.weather.com.cn/weather_img/7.gif】)
{
    "weatherinfo": {
        "city": "金壇",
        "city_en": "jintan",
        "date_y": "2011年10月21日",
        "date": "辛卯年",
        "week": "星期五",
        "fchh": "08",
        "cityid": "101191103",
        "temp1": "23℃~16℃",
        "temp2": "24℃~16℃",
        "temp3": "22℃~12℃",
        "temp4": "18℃~9℃",
        "temp5": "19℃~11℃",
        "temp6": "22℃~13℃",
        "tempF1": "73.4℉~60.8℉",
        "tempF2": "75.2℉~60.8℉",
        "tempF3": "71.6℉~53.6℉",
        "tempF4": "64.4℉~48.2℉",
        "tempF5": "66.2℉~51.8℉",
        "tempF6": "71.6℉~55.4℉",
        "weather1": "小雨轉陰",
        "weather2": "多雲轉晴",
        "weather3": "多雲轉小雨",
        "weather4": "小雨轉陰",
        "weather5": "多雲轉晴",
        "weather6": "多雲轉陰",
        "img1": "7",
        "img2": "2",
        "img3": "1",
        "img4": "0",
        "img5": "1",
        "img6": "7",
        "img7": "7",
        "img8": "2",
        "img9": "1",
        "img10": "0",
        "img11": "1",
        "img12": "2",
        "img_single": "7",
        "img_title1": "小雨",
        "img_title2": "陰",
        "img_title3": "多雲",
        "img_title4": "晴",
        "img_title5": "多雲",
        "img_title6": "小雨",
        "img_title7": "小雨",
        "img_title8": "陰",
        "img_title9": "多雲",
        "img_title10": "晴",
        "img_title11": "多雲",
        "img_title12": "陰",
        "img_title_single": "小雨",
        "wind1": "南風3-4級",
        "wind2": "南風3-4級",
        "wind3": "東南風轉東北風3-4級",
        "wind4": "北風4-5級",
        "wind5": "西北風轉西風3-4級",
        "wind6": "南風轉東風3-4級",
        "fx1": "南風",
        "fx2": "南風",
        "fl1": "3-4級",
        "fl2": "3-4級",
        "fl3": "3-4級",
        "fl4": "4-5級",
        "fl5": "3-4級",
        "fl6": "3-4級",
        "index": "暖",
        "index_d": "較涼爽,建議着長袖襯衫加單褲等春秋過渡裝。年老體弱者宜着針織長袖襯衫、馬甲和長褲。",
        "index48": "熱",
        "index48_d": "天氣較熱,建議着短裙、短褲、短套裝、T恤等夏季服裝。年老體弱者宜着長袖襯衫和單褲。",
        "index_uv": "最弱",
        "index48_uv": "弱",
        "index_xc": "不宜",
        "index_tr": "適宜",
        "index_co": "舒適",
        "st1": "20",
        "st2": "13",
        "st3": "23",
        "st4": "15",
        "st5": "21",
        "st6": "7",
        "index_cl": "較不宜",
        "index_ls": "不宜",
        "index_ag": "易發"
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章