nokogiri excluded elements from select all by class

蹲街弑〆低调 提交于 2019-12-13 03:47:26

问题


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).



来源:https://stackoverflow.com/questions/7102993/nokogiri-excluded-elements-from-select-all-by-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!