Python解析Yahoo的XML格式的天气预报数据
以下是Yahoo天气预报接口xml格式数据:
<rss xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" version="2.0"><channel><title>Yahoo! Weather - Beijing, CN</title><link>http://us.rd.yahoo.com/dailynews/rss/weather/Beijing__CN/*http://weather.yahoo.com/forecast/CHXX0008_c.html</link><description>Yahoo! Weather for Beijing, CN</description><language>en-us</language><lastBuildDate>Mon, 06 Oct 2014 5:00 pm CST</lastBuildDate><ttl>60</ttl><yweather:location city="Beijing" region="" country="China"/><yweather:units temperature="C" distance="km" pressure="mb" speed="km/h"/><yweather:wind chill="20" direction="200" speed="6.44"/><yweather:atmosphere humidity="34" visibility="" pressure="1020.9" rising="0"/><yweather:astronomy sunrise="6:14 am" sunset="5:49 pm"/><image><title>Yahoo! Weather</title><width>142</width><height>18</height><link>http://weather.yahoo.com</link><url>http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif</url></image><item><title>Conditions for Beijing, CN at 5:00 pm CST</title><geo:lat>39.91</geo:lat><geo:long>116.39</geo:long><link>http://us.rd.yahoo.com/dailynews/rss/weather/Beijing__CN/*http://weather.yahoo.com/forecast/CHXX0008_c.html</link><pubDate>Mon, 06 Oct 2014 5:00 pm CST</pubDate><yweather:condition text="Sunny" code="32" temp="20" date="Mon, 06 Oct 2014 5:00 pm CST"/><description><![CDATA[<img src="http://l.yimg.com/a/i/us/we/52/32.gif"/><br /> <b>Current Conditions:</b><br /> Sunny, 20 C<BR /> <BR /><b>Forecast:</b><BR /> Mon - Clear. High: 19 Low: 9<br /> Tue - Sunny. High: 22 Low: 10<br /> Wed - Sunny. High: 24 Low: 12<br /> Thu - Sunny. High: 25 Low: 13<br /> Fri - Partly Cloudy. High: 24 Low: 13<br /> <br /> <a href="http://us.rd.yahoo.com/dailynews/rss/weather/Beijing__CN/*http://weather.yahoo.com/forecast/CHXX0008_c.html">Full Forecast at Yahoo! Weather</a><BR/><BR/> (provided by <a href="http://www.weather.com" >The Weather Channel</a>)<br/>]]></description><yweather:forecast day="Mon" date="6 Oct 2014" low="9" high="19" text="Clear" code="31"/><yweather:forecast day="Tue" date="7 Oct 2014" low="10" high="22" text="Sunny" code="32"/><yweather:forecast day="Wed" date="8 Oct 2014" low="12" high="24" text="Sunny" code="32"/><yweather:forecast day="Thu" date="9 Oct 2014" low="13" high="25" text="Sunny" code="32"/><yweather:forecast day="Fri" date="10 Oct 2014" low="13" high="24" text="Partly Cloudy" code="30"/><guid isPermaLink="false">CHXX0008_2014_10_10_7_00_CST</guid></item></channel></rss><!--fan1587.sports.bf1.yahoo.com Mon Oct 6 03:36:02 PDT 2014-->
针对Yahoo的XML格式的天气预报数据,获取当天和最近几天的天气:
开始解析:
#-*-coding:UTF-8-*-import xml.etree.ElementTree as etreeweatherxml = etree.parse('yahooweather.xml')tree = weatherxml.getroot()for root in tree:# print root #channel 直接的一级子元素pindao = tree.findall('channel')des = pindao[0].find('title')print des.textfor elem in tree.iter(tag='pubDate'): #iter() 深度优先搜素遍历print elem.textfor elem in tree.iter(tag='{http://xml.weather.yahoo.com/ns/rss/1.0}condition'):print elem.attribfor elem in tree.iter(tag='{http://xml.weather.yahoo.com/ns/rss/1.0}forecast'):print elem.attrib
yweather:condition
yweather:forecast
冒号前表示命名空间 通过xml文件可以知道yweather=http://xml.weather.yahoo.com/ns/rss/1.0
结果如下:
Python解析Yahoo的XML格式的天气预报数据的更多相关文章
- python解析Yahoo的XML格式的天气预报,获取当天和近期几天的天气:
下面是接口xml格式数据: <rss xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo=& ...
- python解析VOC的xml文件并转成自己需要的txt格式
在进行神经网络训练的时候,自己标注的数据集往往会有数据量不够大以及代表性不强等问题,因此我们会采用开源数据集作为训练,开源数据集往往具有特定的格式,如果我们想将开源数据集为我们所用的话,就需要对其格式 ...
- XML(php中获取xml文件的方式/ajax获取xml格式的响应数据的方式)
1.XML 格式规范: ① 必须有一个根元素 ② 不可有空格.不可以数字或.开头.大小写敏感 ③ 不可交叉嵌套 ④ 属性双引号(浏览器自动修正成双引号了) ⑤ 特殊符号要使用实体 ⑥ 注释和HTML一 ...
- python cookbook第三版学习笔记七:python解析csv,json,xml文件
CSV文件读取: Csv文件格式如下:分别有2行三列. 访问代码如下: f=open(r'E:\py_prj\test.csv','rb') f_csv=csv.reader(f) for f in ...
- 使用Python解析豆瓣上Json格式数据
现在的API接口多为xml或json,json解析更简洁相对xml来说 以豆瓣的API接口为例,解析返回的json数据: https://api.douban.com/v2/book/1220562 ...
- python接口自动化-发xml格式post请求
前言 post请求相对于get请求多一个body部分,body部分常见的数据类型有以下四种(注意是常见的,并不是只有4种) application/x-www-form-urlencoded appl ...
- Ajax实现异步操作实例_针对XML格式的请求数据
js分类中有一节[原生js异步请求,XML解析]主要说明了js前台是如何处理XML格式请求和如何接受由服务器返回的XML数据的解析,今天我将用一个实例来说明具体要如何操作. 前台的参数类型也是XML使 ...
- FusionCharts-堆栈图、xml格式、刷新数据、添加事件link、传参
*起因* 本来想用Chart.js来搞图表的, 但是来了个新需求,想搞的华丽点,毕竟对Chart.js来说,实现有点难度, *做出的改变* 最终选择了FusionCharts, *难点* 网上关于Fu ...
- Python使用ElementTree美化XML格式
Python中使用ElementTree可以很方便的处理XML,但是产生的XML文件内容会合并在一行,难以看清楚. 如下格式: <root><aa>aatext<cc&g ...
随机推荐
- .net 分割字符串
string a = "1-2-3-4-5-6-7-8-9"; string[] b = a.Split(new Char[] { '-' }); for (int i = 0; ...
- spring 运行时属性值注入
继续spring学习,今天介绍两种外部属性值注入的方式.当你需要读取配置信息时,可以快速读取. 开始之前先创建属性文件site.properties,放在classpath下面 #数据库配置 ### ...
- js中的typeof和instanceof和===
typeof: 用于判断number/string/boolean/underfined类型/function 不能判断:null和object ,不能区分object和Array instanceo ...
- JS在线生成二维码
Js代码 百度云公开下载地址:http://pan.baidu.com/s/1nvjTXB7 Html+Php代码 <volist name="huodong_list" i ...
- Egret学习笔记 (Egret打飞机-8.敌机和主角飞机发射子弹)
经过前面几章的准备,我们差不多已经具备了一个基本的框架,这一章我们就开始添砖加瓦了. 敌机定时发射一个子弹,子弹的方向是从上到下,但是发射子弹的代码应该放在哪儿呢? 从面向对象编程的思想来说,子弹是敌 ...
- # *Qt for Com 交互*
Qt COM Q_PROPERTY ActiveX properties public slots ActiveX methods signals ActiveX events Qt 属性支持的数据类 ...
- aforge 学习-基本图像处理要用的类库
1.图像灰度化:Grayscale.CommonAlgorithms.BT709(3种) FiltersSequence =new FiltersSequence(数组处理函数): 2.二值化(阈值 ...
- fastboot烧写hi3531
Boot Downloading started. Boot 100 % Downloaded. Boot Downloading completed! U-Boot 2010.06 (Jan 04 ...
- freemarker写select组件报错总结(三)
1.错误描述 <html> <head> <meta http-equiv="content-type" content="text/htm ...
- Linux系统挂载NTFS文件系统
今天尝试并成功的将一块500G的移动硬盘挂载到了RHEL5的系统上,甚感欣慰.想到也许以后自己或其他同学们会有类似经历,于是尽量细致的记录于此. 无论是一块安装了Windows/Linu ...