Home > Enterprise >  Nokogiri next element by type
Nokogiri next element by type

Time:09-03

Say I have

<h3></h3>
<h2></h2>
<p></p>

How can I get to the p node from h3

Right now I can only get from doc.css('h3').next_element which doesn't take any arguments and returns the h2 tag.

Is there a way to check node types recursively or is there a method where I can call for example doc.css('h3').next('p')

P.S Of course the HTML I'm parsing is not as simple as the example above.

CodePudding user response:

If you need only one element (not collection), there is at method

And you need selector with general sibling combinator ~

doc.at('h3 ~ p')

If you need collections of such p that go after each h3 tag

doc.css('h3 ~ p')
  • Related