Nokogiri 递归获取所有孩子 [英] Nokogiri recursively get all children

查看:41
本文介绍了Nokogiri 递归获取所有孩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在针对各种 URL 运行一些统计数据.我想找到子元素数量最集中的顶级元素.我想遵循的方法是识别所有顶级元素,然后确定页面上所有元素中属于它的百分比.

I am running some statistics against various URLS. I want to find the top level element with the most concentrated number of children. The method that I would like to follow is to identify all top level elements and then determine what percentage of all the elements on the page belong to it.

  • 递归获取给定元素的所有子元素.

输入:Nokogiri 元素

Inputs: a Nokogiri Element

输出:Nokogiri 元素数组或子元素总数

Outputs: an array of Nokogiri Elements OR the count of total number of children

  • Ruby 1.9.2
  • Nokogiri 宝石
getChildCount(elem)
    children = elem.children
    return 0 unless children and children.count > 0
    child_count = children.count
    children.each do |child|
        child_count += getChildCount(child)
    end
    child_count
end

推荐答案

traverse 方法 递归地将当前节点和所有子节点生成到一个块中.

the traverse method yields the current node and all children to a block, recursively.

# if you would like it to be returned as an array, rather than each node being yielded to a block, you can do this
result = []
doc.traverse {|node| result << node }
result

# or, 
require 'enumerator'
result = doc.enum_for(:traverse).map

这篇关于Nokogiri 递归获取所有孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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