以下是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. C#中引用变量是否应该加ref?

    看如下代码:   void Test(T t); void Test(ref T t); 当T是值类型的时候,很好判断,第一种并不能改变方法外变量的值,需要第二种方法才可以.通过查看IL代码,可以看到 ...

  2. 树莓派系列教程:1.环境与系统,无显示器无键盘无网线联网并使用PuTTy与VNC图形界面远程登录

    本文所需物品清单: Raspberry Pi 3 Model B 主板.SD卡与读卡器(用于烧录系统) 资料整理来源在文尾 需要下载的资源与工具: 推荐系统-Raspbian 树莓派官方深度定制的硬件 ...

  3. REST&RESTFUL

    REST(表征性状态传输,Representational State Transfer)指的是一组架构约束条件和原则.是Roy Fielding博士在2000年他的博士论文中提出来的一种软件架构风格 ...

  4. FZU 2234

    题目为中文,题意略. 这个题目我开始用贪心做bfs两次,这样做是错的,因为两次局部的最优解并不能得出全局的最优解,以下面样例说明: 3 0   10   -1 10   10   10 1   0  ...

  5. MFC窗口创建、销毁消息流程

    MFC应用程序创建窗口的顺序 1.PreCreateWindow()该函数是一个重载函数,在窗口被创建前,可以在该重载函数中改变创建参数,(可以设置窗口风格等等) 2.PreSubclassWindo ...

  6. TCP/IP协议学习和理解

    TCP:Transmission Control Protocol-传输控制协议 IP:Internet Protocol-网络协议 TCP/IP 不是一个协议,而是一个协议族的统称,里面包括了 IP ...

  7. HDFS读写过程

    HDFS的读写过程: 读过程: Client收到用户的读请求——client拿着path向namenode请求文件或者block的datanode列表——client从返回的datanode列表中选择 ...

  8. 【前端】JavaScript中prototype和__proto__的区别

    转载请注明出处:http://www.cnblogs.com/shamoyuu/p/prototype.html 经常有小伙伴问我关于prototype和__proto__的问题,觉得有必要写一篇博客 ...

  9. PL/SQL 基础知识简介

    1.PL/SQL代码块 PL/SQL 代码块是指令的集合,支持所有的DML,NDS,DBMS_SQL,DDL. :DML 是数据操纵语言(Data Manipulation Language)包括,I ...

  10. 如何创建Filter的属性页

    本篇文档我们将要讲述如何给一个filter创建一个属性页,通过CBasePropertyPage基类.这篇文档的实例代码演 示了创建属性页的步骤,这里我们假设我们要创建属性页的视频filter支持饱和 ...