我有这样的事情:
<div id="sub_div">
<span class="subl">
<div class="node">2204830011</span>
<div class="node">1571827122</span>
<div class="node">...</span>
<div class="node">...</span>
<div class="node">...</span>
</span>
<span class="subl">
<div class="node">...</span>
<div class="node">...</span>
<div class="node">...</span>
<div class="node">...</span>
<div class="node">...</span>
</span>
<span class="subl">
<div class="node">...</span>
<div class="node">...</span>
<div class="node">...</span>
</span>
现在,我正在这样做:
def self.parse_nodes
id = @data.at_css("#n_info #clipnode").text unless @data.at_css("#n_info #clipnode").nil?
name = @data.at_css("#n_info .node_name").text unless @data.at_css("#n_info .node_name").nil?
parent = @data.at_css(".bc a").text unless @data.at_css(".bc a").nil?
children_array = []
children = @data.css('#sub_div')
children.css('.subl').each do | child |
child_id = child.css('.node').text[/[\d,]+/].to_i
children_array ||= []
children_array << child_id
end
nodes_hash = "id: #{id}, name: #{name}, parent: #{parent}, children: #{children_array}"
nodes_hash
end
我得到这样的东西:
[220483001115718271223064201115857511158575013463330111571879115709231157103512258019011157197311570657115706941,
220483001115718271223064201115857511158575013463330111571879115709231157103512258019011157197311570657115706941,
220483001115718271223064201115857511158575013463330111571879115709231157103512258019011157197311570657115706941]
我不知道为什么代码会出现.node
三次。但无论如何,我想做的事情是废弃.node
每个.subl
div 中的内容并将它们呈现为一个数组:
[2204830011, 1571827122, 3064201115, 8575111585, 7501346333,
0111571879, 1157092311, 5710351225, 8019011157, 1973115706,
57115706941]