nokogiri 从 select all 中按类排除元素 [英] nokogiri excluded elements from select all by class

查看:56
本文介绍了nokogiri 从 select all 中按类排除元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想从一个节点的所有子节点的选择中按类排除几个子元素

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 

我知道这不是 write 语法,但一直无法找到如何选择元素类,而不是选择和元素 by 类.

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.

推荐答案

css 方法为您提供 Nokogiri::XML::Element 实例,这些实例从它们的 Nokogiri::XML::Node 父类.要从节点中获取属性,请使用 []:

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

您也可以使用 if(child['class'] != 'this' && child['class'] !='that') 如果这对您更有意义.

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

但是,class 属性可以有多个值,因此您可能希望将它们拆分成空白区域:

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

交叉口只是一种简单的方法来查看是否这两个数组有任何共同的元素(即 classes 包含您想要排除的任何内容).

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

这篇关于nokogiri 从 select all 中按类排除元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆