Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the <p> tag parent class name using Nokogiri?

Tags:

ruby

nokogiri

I'm trying to get the <p> tag's parent class name?

<div class="entry-content">
   <p>Some text...</p>
</div>

How can I obtain this?

like image 204
Hugo Sousa Avatar asked Aug 19 '13 16:08

Hugo Sousa


3 Answers

Some find that using css and the nokogiri parent method are easier to read/maintain than xpath:

html = %q{
<div class="entry-content">
   <p>Some text...</p>
</div>
}

doc = Nokogiri::HTML(html)
doc.css('p').each do |p|
    puts p.parent.attr('class')
end
like image 76
Justin Ko Avatar answered Nov 19 '22 04:11

Justin Ko


Use an XPath like //p/.. or //*[p] (the parent of any "p" element at any depth).

str =<<__HERE__
<div class="entry-content">
   <p>Some text...</p>
</div>
__HERE__

html = Nokogiri::HTML(str)
p_parents = html.xpath('//p/..') # => NodeSet containing the "<div>" element.
p_parents.each do |node|
  puts node.attr('class') # => "entry-content"
end
like image 5
maerics Avatar answered Nov 19 '22 02:11

maerics


I would use #at_css,instead of css.

require 'nokogiri'

str =<<__HERE__
<div class="entry-content">
   <p>Some text...</p>
</div>
__HERE__

html = Nokogiri::HTML(str)
p_parent = html.at_css('p').parent
p_parent.name # => "div"
p_parent['class'] # => "entry-content"
like image 2
Arup Rakshit Avatar answered Nov 19 '22 02:11

Arup Rakshit