2011-12-13 42 views
0

我使用libxml来解析这个。我想要yweather:forecast节点。我试过/rss/channel/item/yweather:forecast,但没有奏效。有任何想法吗?什么XPath查询将适用于这些节点?

<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 - Opera, FR</title> 
<link> 
http://us.rd.yahoo.com/dailynews/rss/weather/Opera__FR/*http://weather.yahoo.com/forecast/FRXX2071_f.html 
</link> 
<description>Yahoo! Weather for Opera, FR</description> 
<language>en-us</language> 
<lastBuildDate>Wed, 10 Aug 2011 3:30 am CEST</lastBuildDate> 
<ttl>60</ttl> 
<yweather:location city="Opera" region="" country="FR"/> 
<yweather:units temperature="F" distance="mi" pressure="in" speed="mph"/> 
<yweather:wind chill="50" direction="0" speed="0"/> 
<yweather:atmosphere humidity="87" visibility="6.21" pressure="30.33" rising="2"/> 
<yweather:astronomy sunrise="6:33 am" sunset="9:14 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 Opera, FR at 3:30 am CEST</title> 
<geo:lat>48.87</geo:lat> 
<geo:long>2.34</geo:long> 
<link> 
http://us.rd.yahoo.com/dailynews/rss/weather/Opera__FR/*http://weather.yahoo.com/forecast/FRXX2071_f.html 
</link> 
<pubDate>Wed, 10 Aug 2011 3:30 am CEST</pubDate> 
<yweather:condition text="Fair" code="33" temp="50" date="Wed, 10 Aug 2011 3:30 am CEST"/> 
<description> 
<![CDATA[ 
<img src="http://l.yimg.com/a/i/us/we/52/33.gif"/><br /> <b>Current Conditions:</b><br /> Fair, 50 F<BR /> <BR /><b>Forecast:</b><BR /> Wed - Mostly Sunny. High: 74 Low: 54<br /> Thu - Partly Cloudy. High: 77 Low: 66<br /> Fri - Light Rain. High: 72 Low: 61<br /> Sat - Showers. High: 74 Low: 61<br /> Sun - Rain. High: 71 Low: 60<br /> <br /> <a href="http://us.rd.yahoo.com/dailynews/rss/weather/Opera__FR/*http://weather.yahoo.com/forecast/FRXX2071_f.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="Wed" date="10 Aug 2011" low="54" high="74" text="Mostly Sunny" code="34"/> 
<yweather:forecast day="Thu" date="11 Aug 2011" low="66" high="77" text="Partly Cloudy" code="30"/> 
<yweather:forecast day="Fri" date="12 Aug 2011" low="61" high="72" text="Light Rain" code="11"/> 
<yweather:forecast day="Sat" date="13 Aug 2011" low="61" high="74" text="Showers" code="11"/> 
<yweather:forecast day="Sun" date="14 Aug 2011" low="60" high="71" text="Rain" code="12"/> 
<guid isPermaLink="false">FRXX2071_2011_08_10_3_30_CEST</guid> 
</item> 
</channel> 
</rss> 

回答

1

的XPath是好的,但LXML的.xpath需要适当的命名空间声明:

tree.xpath('/rss/channel/item/yweather:forecast', 
      namespaces={"yweather": "http://xml.weather.yahoo.com/ns/rss/1.0"}) 
+0

看到的,我用这个包装器的libxml:http://cocoawithlove.com/2008/10/using -libxml2-for-parsing-and-xpath.html不幸的是我不知道在哪里添加命名空间。 – Yep 2011-12-13 19:28:01