设为首页 收藏本站
查看: 1689|回复: 0

[经验分享] python解析Yahoo的XML格式的天气预报,获取当天和近期几天的天气:

[复制链接]

尚未签到

发表于 2015-11-30 09:29:06 | 显示全部楼层 |阅读模式
  下面是接口xml格式数据:

<rss xmlns:yweather=&quot;http://xml.weather.yahoo.com/ns/rss/1.0&quot; xmlns:geo=&quot;http://www.w3.org/2003/01/geo/wgs84_pos#&quot; version=&quot;2.0&quot;>
<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=&quot;Beijing&quot; region=&quot;&quot; country=&quot;China&quot;/>
<yweather:units temperature=&quot;C&quot; distance=&quot;km&quot; pressure=&quot;mb&quot; speed=&quot;km/h&quot;/>
<yweather:wind chill=&quot;20&quot; direction=&quot;200&quot; speed=&quot;6.44&quot;/>
<yweather:atmosphere humidity=&quot;34&quot; visibility=&quot;&quot; pressure=&quot;1020.9&quot; rising=&quot;0&quot;/>
<yweather:astronomy sunrise=&quot;6:14 am&quot; sunset=&quot;5:49 pm&quot;/>
<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=&quot;Sunny&quot; code=&quot;32&quot; temp=&quot;20&quot; date=&quot;Mon, 06 Oct 2014 5:00 pm CST&quot;/>
<description>
<![CDATA[
<img src=&quot;http://l.yimg.com/a/i/us/we/52/32.gif&quot;/><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=&quot;http://us.rd.yahoo.com/dailynews/rss/weather/Beijing__CN/*http://weather.yahoo.com/forecast/CHXX0008_c.html&quot;>Full Forecast at Yahoo! Weather</a><BR/><BR/> (provided by <a href=&quot;http://www.weather.com&quot; >The Weather Channel</a>)<br/>
]]>
</description>
<yweather:forecast day=&quot;Mon&quot; date=&quot;6 Oct 2014&quot; low=&quot;9&quot; high=&quot;19&quot; text=&quot;Clear&quot; code=&quot;31&quot;/>
<yweather:forecast day=&quot;Tue&quot; date=&quot;7 Oct 2014&quot; low=&quot;10&quot; high=&quot;22&quot; text=&quot;Sunny&quot; code=&quot;32&quot;/>
<yweather:forecast day=&quot;Wed&quot; date=&quot;8 Oct 2014&quot; low=&quot;12&quot; high=&quot;24&quot; text=&quot;Sunny&quot; code=&quot;32&quot;/>
<yweather:forecast day=&quot;Thu&quot; date=&quot;9 Oct 2014&quot; low=&quot;13&quot; high=&quot;25&quot; text=&quot;Sunny&quot; code=&quot;32&quot;/>
<yweather:forecast day=&quot;Fri&quot; date=&quot;10 Oct 2014&quot; low=&quot;13&quot; high=&quot;24&quot; text=&quot;Partly Cloudy&quot; code=&quot;30&quot;/>
<guid isPermaLink=&quot;false&quot;>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&#26684;式的天气预报,获取当天和近期几天的天气:
  開始解析:

#-*-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
  结果:
DSC0000.jpg

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-145169-1-1.html 上篇帖子: Python——eventlet.wsgi 下篇帖子: Python学习 Day 5 高阶函数 map/reduce filter sorter 返回函数 匿名函数 装饰器 偏函数
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表