获取多个结果xpath div文本和下一个div文本

时间:2014-11-23 22:44:30

标签: xpath yql

使用xpath,我可以一次从页面中获得2个结果吗?例如,使用xpath我可以得到第一个img元素:

xpath='//div[@class="forecast-element graphic-box"]/img

...和下​​一个兄弟类=“forecast-element”。

我尝试了“和”没有成功:

xpath='//div[@class="forecast-element graphic-box"]/img and //div[@class="forecast-element"]'

也:

xpath='//div[@class="forecast-element graphic-box"]/img and following-sibling:://div[@class="forecast-element graphic-box"]'

我有这个HTML:

<div class='forecast-element graphic-box ' style="background-image:url('/assets/images/forecast/BluePattern.png');">
    <h4 style="color: #FFF;">AVALANCHE DANGER <span style="margin-left: 60px;"> MORNING </span><span style="margin-left: 210px;"> AFTERNOON</span></h4>
    <img src="/images/forecast/2014-11-23_teton_hazard.gif" alt="Teton Area avalanche hazard rating for 11/23/2014" />
    <div style='margin: 2px auto;'><a href="/assets/pdfs/North%20American%20Danger%20Scale.pdf" style='font-size: 14px; color: #CCC;'>View full danger scale definitions</a>
    </div>
    <a href="/assets/pdfs/North%20American%20Danger%20Scale.pdf" style='font-size: 14px;'>
        <img src="/assets/images/forecast/DangerScale.png" style="margin-left: 150px; margin-top: 5px;" alt='Avalanche danger scale ratings'>
    </a>
</div>

<div class='forecast-element'>
    <h3 style='margin-bottom: 8px;'>GENERAL AVALANCHE ADVISORY</h3>
    Moderate to heavy snowfall combined with strong southwesterly to northwesterly ridgetop winds have created unstable avalanche conditions. New wind slabs have developed at the mid and upper elevations. Snowfall over the past 24 hours has also added weight to existing weak layers near the base of the snowpack. Early season snowfall can easily cloud people’s judgment. Cautious route finding and conservative decision making will be essential for safe travel in avalanche terrain today.</div>

我想使用follow-sibling,因为该项目位于图形框元素之后,并且html上方和下方还有其他预测元素。顺便说一句,如果有所作为,我使用的是YQL ......

理想情况下,结果将是(假设在这里):

  

xpath imgsrc =“/ images / forecast / 2014-11-23_teton_hazard.gif”

     

xpath text =“GENERAL AVALANCHE   建议适度降雪和强降雪相结合   西南向西北方向........“

谢谢!

1 个答案:

答案 0 :(得分:3)

选择多个节点的正确语法是|

试试这个:

//div[@class="forecast-element graphic-box"]/img | //div[@class="forecast-element"]

正如您所提到的,这些是两个独立的查询元素。要选择以下元素,只需执行此操作

//div[@class="forecast-element graphic-box"]/img | //div[@class="forecast-element graphic-box"]/following-sibling::div[@class="forecast-element"]

在某些xpath解析器中,这也可以工作:

//div[@class="forecast-element graphic-box"]/(img|following-sibling::div[@class="forecast-element"])