cbmeeks
cbmeeks

Reputation: 11420

How do I get the next HTML element in Nokogiri?

Let's say my HTML document is like:

<div class="headline">News</div>
<p>Some interesting news here</p>
<div class="headline">Sports</div>
<p>Baseball is fun!</p>

I can get the headline divs with the following code:

require 'rubygems'
require 'nokogiri'
require 'open-uri'

url = "mypage.html"
doc = Nokogiri::HTML(open(url))

doc.css(".headline").each do |item|  
  puts item.text
end 

But how do I access the content in the following p tag so that News is related to Some interesting news here, etc?

Upvotes: 14

Views: 13994

Answers (1)

Nathan Ostgard
Nathan Ostgard

Reputation: 8416

You want Node#next_element:

doc.css(".headline").each do |item|
  puts item.text
  puts item.next_element.text
end

There is also item.next, but that will also return text nodes, where item.next_element will return only element nodes (like p).

Upvotes: 37

Related Questions