获取最近父标记的内部文本

时间:2015-08-14 16:56:19

标签: html css ruby xpath nokogiri

我使用Ruby& Nokogiri在页面上解析html。

<div><a href="#" title="firstTitle">text one</a></div>

  <p class="OK">some content</p>
  <p class="OK">some content</p>

<div><a href="#" title="secondTitle">text two</a></div>

  <p class="WARNING">some content</p>
  <p class="WARNING">some content</p>

<div><a href="#" title="thirdTitle">text three</a></div>

  <p class="CRITICAL">some content</p>
  <p class="CRITICAL">some content</p>

假设我想查找类WARNING的段落,我可以成功地使用:

doc = Nokogiri::HTML(html)
warning = doc.css('p.WARNING')

但现在我想定位最近的父a标记的内部文字,在这种情况下,它会返回text two

我尝试过使用.first.parent.nameprevious_element等其他版本,但尚未成功,我们非常感谢您提供一些见解。谢谢!

1 个答案:

答案 0 :(得分:1)

You can use XPath preceding-siblng axis to find specific sibling element before the context element. For example, given the context element is <p>, you can try something like the following to get the nearest preceding sibling div and then return the corresponding a element :

link = warning.at_xpath('./preceding-siblng::div[1]/a')
相关问题