以下是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 etree

weatherxml = 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.text

    for elem in tree.iter(tag='pubDate'):    #iter() 深度优先搜素遍历

       print elem.text

    for elem in tree.iter(tag='{http://xml.weather.yahoo.com/ns/rss/1.0}condition'):

       print elem.attrib

    for 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格式的天气预报数据的更多相关文章

  1. python解析Yahoo的XML格式的天气预报,获取当天和近期几天的天气:

    下面是接口xml格式数据: <rss xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo=& ...

  2. python解析VOC的xml文件并转成自己需要的txt格式

    在进行神经网络训练的时候,自己标注的数据集往往会有数据量不够大以及代表性不强等问题,因此我们会采用开源数据集作为训练,开源数据集往往具有特定的格式,如果我们想将开源数据集为我们所用的话,就需要对其格式 ...

  3. XML(php中获取xml文件的方式/ajax获取xml格式的响应数据的方式)

    1.XML 格式规范: ① 必须有一个根元素 ② 不可有空格.不可以数字或.开头.大小写敏感 ③ 不可交叉嵌套 ④ 属性双引号(浏览器自动修正成双引号了) ⑤ 特殊符号要使用实体 ⑥ 注释和HTML一 ...

  4. 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 ...

  5. 使用Python解析豆瓣上Json格式数据

    现在的API接口多为xml或json,json解析更简洁相对xml来说 以豆瓣的API接口为例,解析返回的json数据: https://api.douban.com/v2/book/1220562 ...

  6. python接口自动化-发xml格式post请求

    前言 post请求相对于get请求多一个body部分,body部分常见的数据类型有以下四种(注意是常见的,并不是只有4种) application/x-www-form-urlencoded appl ...

  7. Ajax实现异步操作实例_针对XML格式的请求数据

    js分类中有一节[原生js异步请求,XML解析]主要说明了js前台是如何处理XML格式请求和如何接受由服务器返回的XML数据的解析,今天我将用一个实例来说明具体要如何操作. 前台的参数类型也是XML使 ...

  8. FusionCharts-堆栈图、xml格式、刷新数据、添加事件link、传参

    *起因* 本来想用Chart.js来搞图表的, 但是来了个新需求,想搞的华丽点,毕竟对Chart.js来说,实现有点难度, *做出的改变* 最终选择了FusionCharts, *难点* 网上关于Fu ...

  9. Python使用ElementTree美化XML格式

    Python中使用ElementTree可以很方便的处理XML,但是产生的XML文件内容会合并在一行,难以看清楚. 如下格式: <root><aa>aatext<cc&g ...

随机推荐

  1. hdu3480 Division(dp平行四边形优化)

    题意:将n个数分成m段,每段的代价为最大值减最小值的平方,为代价最小是多少n<=10000 ,m<=5000 题解:先拍好序,从小到大,这样绝对是花费最小的,不过怎么样来做呢?一定很容易想 ...

  2. CentOs下Mongodb的下载与安装

    1.下载MongoDB(64位) http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.4.9.tgz tar zxvf mongodb-lin ...

  3. ZOJ - 3818 字符串

    思路:对于"ABABA"可以先枚举"AB",然后检查下一个串是否等于"AB",剩下的串就是A,最后检查A和B是否相等:对于"ABA ...

  4. nyoj940 A dp problem 打表

    首先枚举i,那么构成i^2的最小值为1个正方形,当然1~1000并不会都得到答案,那么剩下的数字就递增枚举这些数,这个数可能右多对数构成,则枚举这些数.例如 5 = 1 + 4, 5 = 2 + 3, ...

  5. 【CF 678F】Lena and Queries

    Time Limit: 2000 ms   Memory Limit: 512 MB Description 初始有一个空集合 n个操作 有三种操作,如下: 1 a b 表示向集合中插入二元组(a,b ...

  6. spring boot actuator 简单使用

    spring boot admin + spring boot actuator + erueka 微服务监控 简单的spring boot actuator 使用 POM <dependenc ...

  7. Storm业务功能

    监控平台当前使用storm对日志进行流式分析计算,用于支撑监控数据清洗,后来逐渐在storm上搭建起数据在线关联,数据离线关联,明细数据清洗,日志搜索等功能,本章节对各功能进行简要概述. 对storm ...

  8. Hive:子查询

    Hive只支持在FROM子句中使用子查询,子查询必须有名字,并且列必须唯一:SELECT ... FROM(subquery) name ...

  9. java中servletContextListener、httpSessionListener和servletRequestListener使用整理

    在java web应用中,listener监听器似乎是必不可少的,常常用来监听servletContext.httpSession.servletRequest等域对象的创建.销毁以及属性的变化等等, ...

  10. thinphp 整合ueditor

    我的ueditor是部署在public/editor 部署前台页面 <script type="text/javascript" > var UEDITOR_HOME_ ...