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 ...
随机推荐
- Egret学习笔记 (Egret打飞机-7.实现敌机工厂)
在游戏过程之,敌机是源源不断的冲屏幕上方往下飞,如果我们每一架敌机都直接new的话,在飞机很多的情况下,也许有性能问题. 就像前面子弹对象池一样,我们也要实现一个飞机对象池,也就是标题说的敌机工厂(之 ...
- Duilib第一步(II)-Hello World
My first Duilib program 1. Prepare for development 打开DuiFarm项目DuiFarm.cpp文件,将除_tWinMain函数之外所有内容删除.删除 ...
- python开发concurent.furtrue模块:concurent.furtrue的多进程与多线程&协程
一,concurent.furtrue进程池和线程池 1.1 concurent.furtrue 开启进程,多进程&线程,多线程 # concurrent.futures创建并行的任务 # 进 ...
- keepalived双机热备nginx
nginx目前是我最常用的反向代理服务,线上环境为了能更好的应对突发情况,一般会使用keepalived双机热备nginx或者使用docker跑nginx集群,keepalived是比较传统的方式,虽 ...
- 详解Java的自动装箱与拆箱(Autoboxing and unboxing)
一.什么是自动装箱拆箱 很简单,下面两句代码就可以看到装箱和拆箱过程 //自动装箱 Integer total = 99; //自定拆箱 int totalprim = total; 简单一点说,装箱 ...
- zabbix监控安装
1.关闭iptables和selinux # systemctl stop firewalld # systemctl disable firewalld # setenforce 2.安装yum源 ...
- dm642的中断
void fifoint_isr(); extern far void vectors(); void int_init() { IRQ_resetAll(); IRQ_se ...
- CentOS 6.4安装pip,CentOS安装python包管理安装工具pip的方法
CentOS 6.4安装pip,CentOS安装python包管理安装工具pip的方法如下: 截至包子写本文的时候,pip最新为 1.5.5 wget --no-check-certificate h ...
- CF AIM Tech Round 3 (Div. 2) D - Recover the String
模拟 首先可以求出 0 和 1 的个数 之后按照01 10 的个数贪心安排 细节太多 错的都要哭了 #include<bits/stdc++.h> using namespace std; ...
- ASP.Net Core的Code Fist代码先行操作方法
Asp.Net core的Code Fist(代码先行)主要有以下几步: 1.创建实体类 2.创建数据库上下文 3.填加连接字符串 4.依赖注入 5.添加基架工具并执行初始迁移 6搭建模型的基本架构 ...