2011-12-13 2 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

+0

신경 쓰지 말고, 네임 스페이스를 고려하여 라이브러리를 수정하는 방법을 알아 냈습니다. 감사! – Yep

관련 문제