開發一個天氣查詢的工具主要由兩步構成,一是數據的獲取,二是數據的展示。 一、數據獲取 數據獲取又可以分為使用其它公司提供的API和手動抓取其它網站數據。 1. 某公司提供的API 可以從阿裡雲的雲市場中查找,可以找到免費的API,並且提供不同語言的示例,實在不會還可以向客服咨詢... 回想當初使用A ...
開發一個天氣查詢的工具主要由兩步構成,一是數據的獲取,二是數據的展示。
一、數據獲取
數據獲取又可以分為使用其它公司提供的API和手動抓取其它網站數據。
1. 某公司提供的API
可以從阿裡雲的雲市場中查找,可以找到免費的API,並且提供不同語言的示例,實在不會還可以向客服咨詢...
回想當初使用A公司提供的API,使用B公司的APPCODE,還理直氣壯的去問A的客服“我照你們示例寫的為什麼還調用失敗”???
2.其它網站數據抓取
抓取數據源網站為中央氣象臺(http://www.nmc.gov.cn)。利用抓包工具,分析得出獲取實時天氣需要用到以下幾個介面(以查詢天津天氣為例):
1)省及直轄市信息(http://www.nmc.gov.cn/f/rest/province)
GET http://www.nmc.gov.cn/f/rest/province HTTP/1.1
Host: www.nmc.gov.cn
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
Accept-Encoding: gzip, deflate
Referer: http://www.nmc.gov.cn/publish/forecast/ATJ/tian-jin.html
X-Requested-With: XMLHttpRequest
Cookie:UM_distinctid=1608c818855d7-08ea6f54050ee3-4c322e7d-100200-1608c818856e4; CNZZDATA1254743953=65764743-1514184268-https%253A%252F%252Fwww.baidu.com%252F%7C1514184060; followcity=54511%2C58367%2C59493%2C57516%2C58321%2C57679%2C58847%2C59287%2C58238
DNT: 1
Connection: keep-alive
Cache-Control: max-age=0
返回內容為:
[
{
"code": "ABJ",
"name": "北京市",
"url": "/publish/forecast/ABJ.html"
},
{
"code": "ATJ",
"name": "天津市",
"url": "/publish/forecast/ATJ.html"
},
{
"code": "AHE",
"name": "河北省",
"url": "/publish/forecast/AHE.html"
},
{
"code": "ASX",
"name": "山西省",
"url": "/publish/forecast/ASX.html"
},
{
"code": "ANM",
"name": "內蒙古自治區",
"url": "/publish/forecast/ANM.html"
},
{
"code": "ALN",
"name": "遼寧省",
"url": "/publish/forecast/ALN.html"
},
{
"code": "AJL",
"name": "吉林省",
"url": "/publish/forecast/AJL.html"
},
{
"code": "AHL",
"name": "黑龍江省",
"url": "/publish/forecast/AHL.html"
},
{
"code": "ASH",
"name": "上海市",
"url": "/publish/forecast/ASH.html"
},
{
"code": "AJS",
"name": "江蘇省",
"url": "/publish/forecast/AJS.html"
},
{
"code": "AZJ",
"name": "浙江省",
"url": "/publish/forecast/AZJ.html"
},
{
"code": "AAH",
"name": "安徽省",
"url": "/publish/forecast/AAH.html"
},
{
"code": "AFJ",
"name": "福建省",
"url": "/publish/forecast/AFJ.html"
},
{
"code": "AJX",
"name": "江西省",
"url": "/publish/forecast/AJX.html"
},
{
"code": "ASD",
"name": "山東省",
"url": "/publish/forecast/ASD.html"
},
{
"code": "AHA",
"name": "河南省",
"url": "/publish/forecast/AHA.html"
},
{
"code": "AHB",
"name": "湖北省",
"url": "/publish/forecast/AHB.html"
},
{
"code": "AHN",
"name": "湖南省",
"url": "/publish/forecast/AHN.html"
},
{
"code": "AGD",
"name": "廣東省",
"url": "/publish/forecast/AGD.html"
},
{
"code": "AGX",
"name": "廣西壯族自治區",
"url": "/publish/forecast/AGX.html"
},
{
"code": "AHI",
"name": "海南省",
"url": "/publish/forecast/AHI.html"
},
{
"code": "ACQ",
"name": "重慶市",
"url": "/publish/forecast/ACQ.html"
},
{
"code": "ASC",
"name": "四川省",
"url": "/publish/forecast/ASC.html"
},
{
"code": "AGZ",
"name": "貴州省",
"url": "/publish/forecast/AGZ.html"
},
{
"code": "AYN",
"name": "雲南省",
"url": "/publish/forecast/AYN.html"
},
{
"code": "AXZ",
"name": "西藏自治區",
"url": "/publish/forecast/AXZ.html"
},
{
"code": "ASN",
"name": "陝西省",
"url": "/publish/forecast/ASN.html"
},
{
"code": "AGS",
"name": "甘肅省",
"url": "/publish/forecast/AGS.html"
},
{
"code": "AQH",
"name": "青海省",
"url": "/publish/forecast/AQH.html"
},
{
"code": "ANX",
"name": "寧夏回族自治區",
"url": "/publish/forecast/ANX.html"
},
{
"code": "AXJ",
"name": "新疆維吾爾自治區",
"url": "/publish/forecast/AXJ.html"
},
{
"code": "AXG",
"name": "香港特別行政區",
"url": "/publish/forecast/AXG.html"
},
{
"code": "AAM",
"name": "澳門特別行政區",
"url": "/publish/forecast/AAM.html"
},
{
"code": "ATW",
"name": "臺灣省",
"url": "/publish/forecast/ATW.html"
}
]
2)區縣信息(http://www.nmc.gov.cn/f/rest/province/ATJ)
//根據所選省或直轄市生成區縣信息查詢介面
GET http://www.nmc.gov.cn/f/rest/province/ATJ HTTP/1.1
Host: www.nmc.gov.cn
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
Accept-Encoding: gzip, deflate
Referer: http://www.nmc.gov.cn/publish/forecast/ATJ/tian-jin.html
X-Requested-With: XMLHttpRequest
Cookie:UM_distinctid=1608c818855d7-08ea6f54050ee3-4c322e7d-100200-1608c818856e4; CNZZDATA1254743953=65764743-1514184268-https%253A%252F%252Fwww.baidu.com%252F%7C1514184060; followcity=54511%2C58367%2C59493%2C57516%2C58321%2C57679%2C58847%2C59287%2C58238
DNT: 1
Connection: keep-alive
Cache-Control: max-age=0
返回內容為:
[
{
"url": "/publish/forecast/ATJ/xi-qing.html",
"code": "54527",
"city": "西青",
"province": "天津市"
},
{
"url": "/publish/forecast/ATJ/bin-hai-xin-qu.html",
"code": "54623",
"city": "濱海新區",
"province": "天津市"
},
{
"url": "/publish/forecast/ATJ/bao-di.html",
"code": "54525",
"city": "寶坻",
"province": "天津市"
},
{
"url": "/publish/forecast/ATJ/bei-chen.html",
"code": "54528",
"city": "北辰",
"province": "天津市"
},
{
"url": "/publish/forecast/ATJ/da-gang.html",
"code": "54645",
"city": "大港",
"province": "天津市"
},
{
"url": "/publish/forecast/ATJ/dong-li.html",
"code": "54526",
"city": "東麗",
"province": "天津市"
},
{
"url": "/publish/forecast/ATJ/han-gu.html",
"code": "54530",
"city": "漢沽",
"province": "天津市"
},
{
"url": "/publish/forecast/ATJ/ji-xian.html",
"code": "54428",
"city": "薊縣",
"province": "天津市"
},
{
"url": "/publish/forecast/ATJ/jin-nan.html",
"code": "54622",
"city": "津南",
"province": "天津市"
},
{
"url": "/publish/forecast/ATJ/jing-hai.html",
"code": "54619",
"city": "靜海",
"province": "天津市"
},
{
"url": "/publish/forecast/ATJ/ning-he.html",
"code": "54529",
"city": "寧河",
"province": "天津市"
},
{
"url": "/publish/forecast/ATJ/tian-jin.html",
"code": "54517",
"city": "天津",
"province": "天津市"
},
{
"url": "/publish/forecast/ATJ/wu-qing.html",
"code": "54523",
"city": "武清",
"province": "天津市"
}
]
3)某城市當前天氣查詢(http://www.nmc.gov.cn/f/rest/real/54517)
GET http://www.nmc.gov.cn/f/rest/real/54517?_=1514185989171 HTTP/1.1
Host: www.nmc.gov.cn
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
Accept-Encoding: gzip, deflate
Referer: http://www.nmc.gov.cn/publish/forecast/ATJ/tian-jin.html
X-Requested-With: XMLHttpRequest
Cookie:UM_distinctid=1608c818855d7-08ea6f54050ee3-4c322e7d-100200-1608c818856e4; CNZZDATA1254743953=65764743-1514184268-https%253A%252F%252Fwww.baidu.com%252F%7C1514184060; followcity=54511%2C58367%2C59493%2C57516%2C58321%2C57679%2C58847%2C59287%2C58238
DNT: 1
Connection: keep-alive
返回內容為:
{
"station":
{
"url":"/publish/forecast/ATJ/tian-jin.html",
"code":"54517",
"city":"天津",
"province":"天津市"
},
"publish_time":"2017-12-25 15:05",
"weather":
{
"temperature":5.4, //氣溫 ℃
"airpressure":1023.0, //氣壓
"humidity":15.0, //濕度 %
"rain":0.0, //降水量 mm
"rcomfort":43,
"icomfort":-2,
"info":"晴",
"img":"0",
"feelst":6.7//體感溫度 ℃
},
"wind":
{
"direct":"西南風",
"power":"微風",
"speed":2.8 m/s
},
"warn":
{
"alert":"2017年12月25日14時天津市發佈海上大風藍色預警",
"pic":"http://image.nmc.cn/static/site/nmc/themes/basic/alarm/p.png",
"province":"天津市",
"city":"9999",
"url":"/f/alarm/12000041600000_20171225145105.html",
"issuecontent":"天津海洋中心氣象臺於2017年12月25日14時43分發佈海上大風藍色預警信號:預計今天後半夜到明天白天,渤海西部中部海面將有東到東北風7級,陣風8級,請有關單位和人員作好防範準備。",
"fmeans":"9999"
}
}
介面分析完畢,接下來就是利用代碼獲取數據的過程,以獲取省及直轄市數據為例,代碼如下所示:
/// <summary> /// 獲取省及直轄市 /// </summary> public List<ProvinceModel> GetProvinceData(ref string errorMsg) { string strResult = string.Empty; try { #region HttpWebRequest HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://www.nmc.gov.cn/f/rest/province"); string cookie = "UM_distinctid=1608c818855d7-08ea6f54050ee3-4c322e7d-100200-1608c818856e4; CNZZDATA1254743953=65764743-1514184268-https%253A%252F%252Fwww.baidu.com%252F%7C1514184060; followcity=54511%2C58367%2C59493%2C57516%2C58321%2C57679%2C58847%2C59287%2C58238"; string referer = "http://www.nmc.gov.cn/publish/forecast/ATJ/tian-jin.html"; HttpWebRequestHelper.HttpWebRequestConfig(request, cookie, referer); #endregion using (WebResponse response = request.GetResponse()) { Stream respStream = response.GetResponseStream(); bool isNeedDeCompress = response.Headers.ToString().Contains("Content-Encoding: gzip"); if (isNeedDeCompress) respStream = new GZipStream(respStream, CompressionMode.Decompress); StreamReader reader = new StreamReader(respStream, Encoding.UTF8); strResult = reader.ReadToEnd(); List<ProvinceModel> lstModel = JsonConvert.DeserializeObject<List<ProvinceModel>>(strResult); response.Close(); return lstModel; } } catch (Exception ex) { errorMsg = ex.Message; } return null; } //其中HttpWebRequestHelper.HttpWebRequestConfig()是對請求頭的一些設置。
二、數據展示
數據得到之後,先是模仿原網站的樣式用WPF簡單的實現了一版,運行圖如下:
最近工作不忙,感覺好久沒搞WebForm,所以就又簡單的做了下麵這個: