首页 > 代码库 > C# 解析百度天气数据,Rss解析百度新闻以及根据IP获取所在城市
C# 解析百度天气数据,Rss解析百度新闻以及根据IP获取所在城市
百度天气
接口地址:http://api.map.baidu.com/telematics/v3/weather?location=上海&output=json&ak=hXWAgbsCC9UTkBO5V5Qg1WZ9,其中ak是密钥,自行去申请即可,便于大家测试,楼主就公布并了自己的Key,这样可以直接获取到数据。
获取到的数据是这样的:
{"error":0,"status":"success","date":"2014-10-27","results":[{"currentCity":"上海","pm25":"95","index":[{"title":"穿衣","zs":"较舒适","tipt":"穿衣指数","des":"建议着薄外套、开衫牛仔衫裤等服装。年老体弱者应适当添加衣物,宜着夹克衫、薄毛衣等。"},{"title":"洗车","zs":"较适宜","tipt":"洗车指数","des":"较适宜洗车,未来一天无雨,风力较小,擦洗一新的汽车至少能保持一天。"},{"title":"旅游","zs":"适宜","tipt":"旅游指数","des":"天气较好,温度适宜,但风稍微有点大。这样的天气适宜旅游,您可以尽情地享受大自然的无限风光。"},{"title":"感冒","zs":"较易发","tipt":"感冒指数","des":"天气较凉,较易发生感冒,请适当增加衣服。体质较弱的朋友尤其应该注意防护。"},{"title":"运动","zs":"较适宜","tipt":"运动指数","des":"天气较好,但风力较大,推荐您进行室内运动,若在户外运动请注意防风。"},{"title":"紫外线强度","zs":"弱","tipt":"紫外线强度指数","des":"紫外线强度较弱,建议出门前涂擦SPF在12-15之间、PA+的防晒护肤品。"}],"weather_data":[{"date":"周一 10月27日 (实时:19℃)","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/duoyun.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/duoyun.png","weather":"多云","wind":"东北风3-4级","temperature":"21 ~ 16℃"},{"date":"周二","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/duoyun.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/yin.png","weather":"多云转阴","wind":"东风微风","temperature":"21 ~ 17℃"},{"date":"周三","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/xiaoyu.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/xiaoyu.png","weather":"小雨","wind":"东风微风","temperature":"21 ~ 19℃"},{"date":"周四","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/xiaoyu.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/xiaoyu.png","weather":"小雨","wind":"东南风微风","temperature":"23 ~ 20℃"}]}]}
根据返回的Json定义出相应的数据结构:
public class BaiduTQ { public int error { get; set; } public string status { get; set; } public string date { get; set; } public List<BaiduResult> results { get; set; } } public class BaiduResult { public string currentCity { get; set; } public string pm25 { get; set; } public List<BaiduIndex> index { get; set; } public List<BaiDuWeaterData> weather_data { get; set; } } public class BaiduIndex { public string title { get; set; } public string zs { get; set; } public string tipt { get; set; } public string des { get; set; } } public class BaiDuWeaterData { public string date { get; set; } public string dayPictureUrl { get; set; } public string nightPictureUrl { get; set; } public string weather { get; set; } public string wind { get; set; } public string temperature { get; set; } }
然后直接通过Newtonsoft.Json 反序列化成即可。
既然是获取天气,肯定是希望获取客户所在城市的天气,下一步则是需要根据用户机器IP获取所在城市,然后获取该城市的天气信息。
IP获取城市
通过淘宝的IP库,http://ip.taobao.com/,即可查询指定IP所在的城市、国家、运营商等。
有了上面的途径,我们下一步的工作就是获取客户的外网IP,而外网IP,是机器连接外网才会有,所以楼主写了一个页面,部署在外网服务器。
相关代码如下:
var ip = Request.UserHostAddress; using (var client = new WebClient()) { var url = "http://ip.taobao.com/service/getIpInfo.php?ip=" + ip; client.Encoding = Encoding.UTF8; var str = client.DownloadString(url); Response.Write(str); }
这样我们就可以获取到客户所在城市的天气数据了。
获取百度新闻
最近还有个小需求,获取某某新闻数据,楼主习惯性的查了下百度的相关资料,能通过Rss来获取百度新闻数据。
接口地址:http://news.baidu.com/n?cmd=7&loc=0&name=%B1%B1%BE%A9&tn=rss
打开后,查看它的源,无非就是xml文件,我们可以将xml文件,序列化成对象,如果没有接触过这类知识,可以看下《xml与对象的序列化和反序列化》。
根据它的源,就能轻松定义出数据结构。
[XmlRoot("rss")] public class Rss { public Channel channel { get; set; } } [XmlRoot("channel")] public class Channel { public string title { get; set; } public BaiduImage image { get; set; } public string link { get; set; } public string description { get; set; } public string language { get; set; } public string lastBuildDate { get; set; } public string docs { get; set; } public string generator { get; set; } [XmlElement] public List<Channel_Item> item { get; set; } } public class BaiduImage { public string title { get; set; } public string link { get; set; } public string url { get; set; } } public class Channel_Item { public string title { get; set; } public string link { get; set; } public string pubDate { get; set; } public string guid { get; set; } public string source { get; set; } public string author { get; set; } public string description { get; set; } }
序列化的方法很简单。
/// <summary> /// 反序列化 /// </summary> public static T Deserialize<T>(string xmlContent) { XmlSerializer xs = new XmlSerializer(typeof(T)); using (StringReader strReader = new StringReader(xmlContent)) { XmlReader xmlReader = XmlReader.Create(strReader); return (T)xs.Deserialize(xmlReader); } }
相关测试代码如下:
一键下载
C# 解析百度天气数据,Rss解析百度新闻以及根据IP获取所在城市