接口地址: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

C# 解析百度天气数据,Rss解析百度新闻以及根据IP获取所在城市相关推荐

  1. 【Android RTMP】音频数据采集编码 ( AAC 音频格式解析 | FLV 音频数据标签解析 | AAC 音频数据标签头 | 音频解码配置信息 )

    文章目录 安卓直播推流专栏博客总结 一. AAC 音频格式解析 二. FLV 音频数据标签解析 1. 分析 FLV 格式中的 AAC 音频格式数据 2. AAC 音频特殊配置 3. AAC 音频数据标 ...

  2. android百度天气接口api接口,百度天气接口api

    百度天气接口 以GET形式提交,返回JSON或XML URL:http://api.map.baidu.com/telematics/v3/weather?location={城市名}&out ...

  3. 获取当前客户端ip,并且根据ip获取当前城市和天气

    获取天气的接口来自网友,我是连接地址 获取当前客户端真实ID:(此方法不可用,为网络爬虫)如果百度找不到方法的朋友来着看看吧https://www.cnblogs.com/pojdd/p/864761 ...

  4. android 百度天气api,9.1 百度免费天气API介绍

    百度免费天气API介绍 百度的API Store 为开发者提供最全面的API服务,汇集了国内外应用开发所需要的Android API/SDK,IOS SDK,涉及设计开发,运维管理,云服务,app推广 ...

  5. Python解析SWAN气象雷达数据--(解析、生成ASCII、Image、netCDF)

    解析 from datetime import * import time import calendar import json import numpy as np from struct imp ...

  6. php调用百度天气接口,php使用百度天气接口示例

    注意地区要转码的 百度ak申请地址:http://lbsyun.baidu.com/apiconsole/key 复制代码 代码如下: $city="嘉兴"; $content = ...

  7. 百度天气api android,为什么百度apistore的天气api在Java项目中能用而在安卓项目中不能用?...

    经测试,在Android中也可以正常获取到数据. @Override protected void onCreate(Bundle savedInstanceState) { super.onCrea ...

  8. python爬取百度地图数据_Python利用百度地图爬取商家信息

    import requests import json import pandas as pd def request_hospital_data(): ak="换成自己的 AK" ...

  9. 高德地图获取所在城市POI数据

    由于小论文方向为电动汽车的路径规划,所以需要用到充电站的数据,因此需要从高德地图上爬充电站数据.本文学习自这篇文章. 高德有一个开放平台,点击进入,注册(登录)(有了账号之后返回首页)-控制台-应用管 ...

最新文章

  1. 读数据库遇到空就进行不下去_如何解决高并发场景下缓存+数据库双写不一致问题?...
  2. MoinMoin Wiki 1.7优化与维护经验
  3. TCP局域网 通讯 的消息发送
  4. java在控制台输出sql_logger4j配置数据库sql输出控制台
  5. javascript动态添加删除行后行内计算及取值
  6. Jmeter3.1官方自带的HTML图形测试报告 + 汉化
  7. ROS学习笔记01:安装ROS - 玩小海龟
  8. js layui跳转页面_【WEB前端开辟】layui的iframe跳转链接与页面按钮跳转相干引见...
  9. ios添加GPUImage到项目
  10. c语言sqrt函数无作用,如何在不使用C语言的sqrt函数的情况下获得数字的平方根...
  11. JAVA 连接sftp服务器,用户名密码方式链接(类似于FinalShell以ssh方式链接LINUX)
  12. 【数字通信】Matlab实现16QAM调制与解调,判决,误码率计算
  13. 一口一口吃掉Struts(六)——动态ActionForm
  14. python爬京东联盟_PHP调用京东联盟开普勒、宙斯API模板
  15. 互联网人吐槽互撕系列漫画 要来啦~
  16. 沙盘游戏模型的基本象征
  17. javac不是内部或外部命令,也不是可运行的程序 或批处理文件的细节问题(window10)
  18. 免费CMS插件文章采集伪原创发布插件
  19. AE+VS+c#开发颜色符号系统之点值符号化(七)
  20. 罗马数字和阿拉伯数字之间的转换

热门文章

  1. 计算机类晋升高级职称业绩报告,高级职称评审,业绩材料怎么写?
  2. 从Excel薪资总表中拆分成各部门的表
  3. Java ArrayList 类
  4. linux 下配置 openOffice ,jodconverter,解决字体乱码
  5. 自适应二次元可爱萝莉音乐404页面源码
  6. (努比亚)手机键盘恢复
  7. 什么是云计算机办公软件,分享5个免费的电脑办公软件,让你的办公效率翻上一倍...
  8. 互联网晚报 | 10月12日 星期二 | 小鹏汽车第10万辆整车正式下线;华为获批生产医疗器械;全国淘宝村数量突破7000...
  9. 元宇宙是什么?如何在元宇宙时代不被割韭菜?
  10. 计算机知识和操作技能,《计算机基础知识与操作技能》毕文才 编_孔网