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 ...
随机推荐
- gerrit+nginx+centos安装配置
安装环境 centos 6.8 gerrit-full-2.5.2.war 下载地址:https://gerrit-releases.storage.googleapis.com/gerrit-ful ...
- PHP中::的使用
访问静态变量,静态属性,const修饰的变量.
- BloomFilter(布隆过滤器)
原文链接:http://blog.csdn.net/qq_38646470/article/details/79431659 1.概念: 如果想判断一个元素是不是在一个集合里,一般想到的是将所有元素保 ...
- hbase 命令
HBase是Google Bigtable的开源实现,它利用Hadoop HDFS作为其文件存储系统,利用Hadoop MapReduce来处理HBase中的海量数据,利用Zookeeper作为协同服 ...
- java执行多条SQL语句
一次执行多条SQL的技术要点如下: DatabaseMetaData接口是描述有关数据库的整体综合信息,由于DatabaseMetaData是接口,所以没有构造方法,故不能使用new来创建Databa ...
- (转载)windows下mysql忘记密码
Mysql版本:5.1 1. 首先检查mysql服务是否启动,若已启动则先将其停止服务,可在开始菜单的运行,使用命令: net stop mysql 打开第一个cmd窗口,切换到mysql的bin目录 ...
- hive:join操作
hive的多表连接,都会转换成多个MR job,每一个MR job在hive中均称为Join阶段.按照join程序最后一个表应该尽量是大表,因为join前一阶段生成的数据会存在于Reducer 的bu ...
- centos7安装zabbix3.2.4
系统:CentOS Linux release 7.2.1511 (Core) zabbix:3.2.4 一.yum -y install httpd mysql mysql-server mysql ...
- ASP.NET没有魔法——ASP.NET MVC 模型绑定解析(下篇)
上一篇<ASP.NET没有魔法——ASP.NET MVC 模型绑定解析(上篇)>文章介绍了ASP.NET MVC模型绑定的相关组件和概念,本章将介绍Controller在执行时是如何通过这 ...
- Nginx HTTP模块指令
alias 指令 该指令用于在url和系统路径之间的映射. location /a/{ alias /b/; } error_page 定义错误页面 error_page 404 /404.html; ...