python解析Yahoo的XML格式的天气预报,获取当天和近期几天的天气:
下面是接口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格式的天气预报数据
以下是Yahoo天气预报接口xml格式数据: <rss xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xm ...
- python解析VOC的xml文件并转成自己需要的txt格式
在进行神经网络训练的时候,自己标注的数据集往往会有数据量不够大以及代表性不强等问题,因此我们会采用开源数据集作为训练,开源数据集往往具有特定的格式,如果我们想将开源数据集为我们所用的话,就需要对其格式 ...
- 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 ...
- Python使用ElementTree美化XML格式
Python中使用ElementTree可以很方便的处理XML,但是产生的XML文件内容会合并在一行,难以看清楚. 如下格式: <root><aa>aatext<cc&g ...
- python 解析与生成xml
xml.etree.ElementTree模块为xml文件的提取和建立提供了简单有效的API.下文中使用ET来代表xml.etree.ElementTree模块. XML是一种内在的分层的数据形式,展 ...
- python利用lxml读写xml格式文件
之前在转换数据集格式的时候需要将json转换到xml文件,用lxml包进行操作非常方便. 1. 写xml文件 a) 用etree和objectify from lxml import etree, o ...
- 【TensorFlow】Python解析xml文件
最近在项目中使用TensorFlow训练目标检测模型,在制作自己的数据集时使用了labelimg软件对图片进行标注,产生了VOC格式的数据,但标注生成的xml文件标签值难免会产生个别错误造成程序无法跑 ...
随机推荐
- 虚拟机VM10连衣裙Mac OS X 10.9.3
最近WWDC终极大招释放--新的编程语言Swift(迅速),导致大波浪,渴望围观程序猿.当然,工欲善其事,其利润,因此,对于那些谁不Mac非常为难.可是,请放心.本文教你怎样在Windows下也能体验 ...
- iframe滚动条问题:显示/隐藏滚动条
iframe 问题2008-01-22 16:37****** 显示 iframe 内容 XHTML 1.0 Transitional 标准不能显示 <!DOCTYPE html PUBLI ...
- hadoop出现namenode running as process 18472. Stop it first.
hadoop出现namenode running as process 18472. Stop it first.等等,类别似几个的出现. namenode running as process 32 ...
- [three.js] 地图不能解决重复的问题 Solving with Texture RepeatWrapping Fail Issue
有些事情,如果你正在寻找侯,怎么也找不到. 有的东西,不经意间,到处: 我认为这是生活中常有的事. 然而,在互联网的浩瀚大海,这同样适用. 正常的一小会儿的积累, 投入少, 积累, 洋大海, 载起一帆 ...
- 第三篇——第二部分——第一文 SQL Server镜像简介
原文:第三篇--第二部分--第一文 SQL Server镜像简介 原文出处:http://blog.csdn.net/dba_huangzj/article/details/26951563 镜像是什 ...
- 如何获得SQL Server索引使用情况
原文:如何获得SQL Server索引使用情况 原文出自: http://www.mssqltips.com/sqlservertip/1239/how-to-get-index-usage-info ...
- js 模块化的规范
The Module Pattern,模块模式,也译为模组模式,是一种通用的对代码进行模块化组织与定义的方式.这里所说的模块(Modules),是指实现某特定功能的一组方法和代码.许多现 ...
- 致网友Wonderfei的一封信(怎样选择自己主动化框架的几点拙见)
注:本来这封信要发给Wonerfei网友的,可是由于每次仅仅能发200字,所以干脆贴到博客上,叫Wonderfei同学到这上面来看,也算是我自己的一个暂时总结吧.同一时候也希望大家给予Wonderfe ...
- Linux概念架构的理解(转)
英文原文:Conceptual Architecture of the Linux Kernel 摘要 Linux kernel成功的两个原因:(1)架构设计支持大量的志愿开发者加入到开发过程中:(2 ...
- 在Mac OS上配置Android开发环境
1)安装配置NDK 1.1 下载NDK并解压缩 下载路径 https://developer.android.com/tools/sdk/ndk/index.html 在terminal运行: chm ...