Currently, I am utilizing Ruby and Nokogiri to extract information from an HTML page:
<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>
If I aim to locate paragraphs with a class of WARNING
, the following code snippet achieves that successfully:
doc = Nokogiri::HTML(html)
warning = doc.css('p.WARNING')
My current challenge is targeting the text within the nearest parent a
tag. To clarify, in this instance, it should return text two
.
I have experimented with methods like .first.parent.name
and previous_element
but have faced obstacles. Any suggestions would be greatly appreciated. Thank you!