Какой запрос XPath будет работать для этих узлов? - PullRequest
0 голосов
/ 13 декабря 2011

Я использую libxml для анализа этого. Я хочу yweather:forecast узлы. Я пытался /rss/channel/item/yweather:forecast, но это не сработало. Есть идеи?

<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 Ответ

1 голос
/ 13 декабря 2011

XPath в порядке, но LXML .xpath требует правильных объявлений пространства имен:

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