开发者

nokogiri excluded elements from select all by class

开发者 https://www.devze.com 2023-03-29 11:37 出处:网络
I\'m just trying to exclude a couple of child elements by class from a selection of all the children of a node

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')
 开发者_如何学Go   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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消