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":"较适宜洗车,未来一天无雨,<span style="width: auto; height: auto; float: none;" id="0_nwp"><a style="text-decoration: none;" mpid="0" target="_blank" href="http://cpro.baidu.com/cpro/ui/uijs.php?c=news&cf=1001&ch=0&di=128&fv=16&jk=f75039926fbab791&k=%B7%E7%C1%A6&k0=%B7%E7%C1%A6&kdi0=0&luki=5&n=10&p=baidu&q=00007110_cpr&rb=0&rs=1&seller_id=1&sid=91b7ba6f923950f7&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1704338&u=http%3A%2F%2Fwww%2Edaxueit%2Ecom%2Farticle%2F5477%2Ehtml&urlid=0" id="0_nwl"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">风力</span></a></span>较小,擦洗一新的汽车至少能保持一天。"},{"title":"旅游","zs":"适宜","tipt":"旅游指数","des":"天气较好,温度适宜,但风稍微有点大。这样的天气适宜旅游,您可以尽情地享受大自然的无限风光。"},{"title":"感冒","zs":"较易发","tipt":"感冒指数","des":"天气较凉,较易发生感冒,请适当增加衣服。体质较弱的朋友尤其应该注意防护。"},{"title":"运动","zs":"较适宜","tipt":"运动指数","des":"天气较好,但风力较大,推荐您进行室内运动,若在户外运动请注意防风。"},{"title":"<span style="width: auto; height: auto; float: none;" id="1_nwp"><a style="text-decoration: none;" mpid="1" target="_blank" href="http://cpro.baidu.com/cpro/ui/uijs.php?c=news&cf=1001&ch=0&di=128&fv=16&jk=f75039926fbab791&k=%D7%CF%CD%E2%CF%DF&k0=%D7%CF%CD%E2%CF%DF&kdi0=0&luki=3&n=10&p=baidu&q=00007110_cpr&rb=0&rs=1&seller_id=1&sid=91b7ba6f923950f7&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1704338&u=http%3A%2F%2Fwww%2Edaxueit%2Ecom%2Farticle%2F5477%2Ehtml&urlid=0" id="1_nwl"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">紫外线</span></a></span>强度","zs":"弱","tipt":"紫外线强度指数","des":"紫外线强度较弱,建议出门前涂擦SPF在12-15之间、PA+的防晒护肤品。"}],"weather_data":[{"date":"周一 10月27日 (实时:19℃)","<span style="width: auto; height: auto; float: none;" id="2_nwp"><a style="text-decoration: none;" mpid="2" target="_blank" href="http://cpro.baidu.com/cpro/ui/uijs.php?c=news&cf=1001&ch=0&di=128&fv=16&jk=f75039926fbab791&k=day&k0=day&kdi0=0&luki=2&n=10&p=baidu&q=00007110_cpr&rb=0&rs=1&seller_id=1&sid=91b7ba6f923950f7&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1704338&u=http%3A%2F%2Fwww%2Edaxueit%2Ecom%2Farticle%2F5477%2Ehtml&urlid=0" id="2_nwl"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">day</span></a></span>PictureUrl":"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":"周三","<span style="width: auto; height: auto; float: none;" id="3_nwp"><a style="text-decoration: none;" mpid="3" target="_blank" href="http://cpro.baidu.com/cpro/ui/uijs.php?c=news&cf=1001&ch=0&di=128&fv=16&jk=f75039926fbab791&k=day&k0=day&kdi0=0&luki=2&n=10&p=baidu&q=00007110_cpr&rb=0&rs=1&seller_id=1&sid=91b7ba6f923950f7&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1704338&u=http%3A%2F%2Fwww%2Edaxueit%2Ecom%2Farticle%2F5477%2Ehtml&urlid=0" id="3_nwl"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">day</span></a></span>PictureUrl":"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 <span style="width: auto; height: auto; float: none;" id="7_nwp"><a style="text-decoration: none;" mpid="7" target="_blank" href="http://cpro.baidu.com/cpro/ui/uijs.php?c=news&cf=1001&ch=0&di=128&fv=16&jk=f75039926fbab791&k=day&k0=day&kdi0=0&luki=2&n=10&p=baidu&q=00007110_cpr&rb=0&rs=1&seller_id=1&sid=91b7ba6f923950f7&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1704338&u=http%3A%2F%2Fwww%2Edaxueit%2Ecom%2Farticle%2F5477%2Ehtml&urlid=0" id="7_nwl"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">day</span></a></span>PictureUrl { 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 <span style="width: auto; height: auto; float: none;" id="4_nwp"><a style="text-decoration: none;" mpid="4" target="_blank" href="http://cpro.baidu.com/cpro/ui/uijs.php?c=news&cf=1001&ch=0&di=128&fv=16&jk=f75039926fbab791&k=xml&k0=xml&kdi0=0&luki=6&n=10&p=baidu&q=00007110_cpr&rb=0&rs=1&seller_id=1&sid=91b7ba6f923950f7&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1704338&u=http%3A%2F%2Fwww%2Edaxueit%2Ecom%2Farticle%2F5477%2Ehtml&urlid=0" id="4_nwl"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">xml</span></a></span>Content)
{
    XmlSerializer xs = new XmlSerializer(typeof(T));
    using (StringReader strReader = new StringReader(xmlContent))
    {
        XmlReader xmlReader = XmlReader.Create(strReader);
        return (T)xs.Deserialize(xmlReader);
    }
}

相关测试代码如下:
见个人CSDN下载中心
http://download.csdn.net/detail/u010416101/8532211

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值