我使用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.name
和previous_element
等其他版本,但尚未成功,我们非常感谢您提供一些见解。谢谢!
答案 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')