nokogiri excluded elements from select all by clas

2019-09-08 16:02发布

问题:

I'm just trying to exclude a couple of child elements by class from a selection of all the children of a node

page.css('div.parent > *').each do |child|
  if (child["class"].content != 'this' && child["class"].content != 'that')
    array.push(child.text.to_s)
  end
end 

I know this not the write syntax, but have been unable to find how to select an elements class, as opposed to selects and element by class.

回答1:

The css method gives you Nokogiri::XML::Element instances and those get most of their behavior from their Nokogiri::XML::Node parent class. To get an attribute out of a Node, use []:

page.css('div.parent > *').each do |child|
  if(!%w{this that}.include?(child['class']))
    array.push(child.text.to_s)
  end
end

You could also use if(child['class'] != 'this' && child['class'] != 'that') if that makes more sense to you.

However, class attributes can have multiple values so you might want to split them into pieces on whitespace:

exclude = %w{this that}
page.css('div.parent > *').each do |child|
  classes = (child['class'] || '').split(/\s+/)
  if((classes & exclude).length > 0)
    array.push(child.text.to_s)
  end
end

The intersection is just an easy way to see if the two arrays have any elements in common (i.e. classes contains anything that you want to exclude).



标签: ruby nokogiri