Python解析Yahoo的XML格式的天气预报数据
2014-10-07 15:15
501 查看
以下是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.attribyweather:conditionyweather:forecast 冒号前表示命名空间 通过xml文件可以知道yweather=http://xml.weather.yahoo.com/ns/rss/1.0结果如下:
相关文章推荐
- python解析Yahoo的XML格式的天气预报,获取当天和近期几天的天气:
- python用json解析爬取天气预报数据
- 利用百度车联网提供的天气查询接口用python查询天气信息以及安卓(Java)利用gson解析数据
- Android解析聚合数据之天气预报
- <WP7>(四)手把手教你写天气预报程序:本地数据库SQL CE,XML数据解析
- <WP7>(三)手把手教你写天气预报程序:运用WebClient获取天气和json数据解析
- Python天气预报数据获取脚本
- 【Android应用开发】-(17)获取Google天气预报通过Xml和JSON解析数据
- Qt实现简易天气预报(解析的是json数据)
- Gson解析第三方提供Json数据(天气预报,新闻等)
- <WP7>(四)手把手教你写天气预报程序:本地数据库SQL CE,XML数据解析
- Android阶段学习笔记 7.25-7.29 之 解析聚合数据 全国天气预报 Json
- python解析json格式的天气数据
- Python爬取天气预报数据,并存入到本地EXCEL中
- android JSON解析数据 android解析天气预报
- <WP7>(三)手把手教你写天气预报程序:运用WebClient获取天气和json数据解析
- Gson解析第三方提供Json数据(天气预报,新闻等)
- 可以显示九天天气情况的天气预报哦-LINUX下JAVA自制服务器端,申请公网IP,处理数据。。第二个项目
- 天气预报数据查询接口
- 利用yahoo天气预报API创建自己的WCF服务