sunyke 发表于 2015-11-30 09:29:06

python解析Yahoo的XML格式的天气预报,获取当天和近期几天的天气:

  下面是接口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 Oct6 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.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
  结果:
页: [1]
查看完整版本: python解析Yahoo的XML格式的天气预报,获取当天和近期几天的天气: