gpt4 book ai didi

ruby - 递归向下钻取 Nokogiri 树

转载 作者:数据小太阳 更新时间:2023-10-29 08:51:15 24 4
gpt4 key购买 nike

<分区>

我想向下钻取树,并存储所有级别:

search_q = Regex.new("Some search regex here")
#something like: page.search('body').first.children.select {|x| x.text[search_q]}.first.children.select {|x| x.text[search_q]}.first......ad infinitum.

我做了一个黑客:

arbitrarily_long_number = 100
drill = []
(0..arbitrarily_long_number).collect do |n|
begin
drill << eval("page.search('body')"+".first.children.select {|x| x.text[search_q]}" * n)
rescue
break
end
end

问题是这仅通过“第一个”选择进行钻取。有没有办法让它钻透每一个节点?我正在考虑某种注入(inject)功能,但我仍然没有全神贯注。任何帮助将不胜感激。

输出:

pp drill[-4]
puts
pp drill[-3]
puts
pp drill[-2]
#=>[#(Element:0x3fc2324522b4 {
name = "u",
children = [
#(Element:0x3fc232060b60 {
name = "span",
attributes = [
#(Attr:0x3fc2320603e0 {
name = "style",
value = "font-size: large;"
})],
children = [ #(Text "Ingredients:")]
})]
})]

[#(Element:0x3fc232060b60 {
name = "span",
attributes = [
#(Attr:0x3fc2320603e0 { name = "style", value = "font-size: large;" })],
children = [ #(Text "Ingredients:")]
})]

[#(Text "Ingredients:")]

注意事项:我正在使用 mechanize gem,它利用了 Nokogiri。 http://mechanize.rubyforge.org/Mechanize/Page.html#method-i-searchhttp://nokogiri.org/Nokogiri/XML/Node.html#method-i-search

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com