需要与'澄清;每个do';我的ruby代码中的块
给定一个html文件:需要与'澄清;每个do';我的ruby代码中的块,ruby,xpath,nokogiri,Ruby,Xpath,Nokogiri,给定一个html文件: <div> <div class="NormalMid"> <span class="style-span"> "Data 1:" <a href="http://site.com/data/1">1</a> <a href="http://site.com/data/2">2</a>
<div>
<div class="NormalMid">
<span class="style-span">
"Data 1:"
<a href="http://site.com/data/1">1</a>
<a href="http://site.com/data/2">2</a>
</span>
</div>
...more divs
<div class="NormalMid">
<span class="style-span">
"Data 20:"
<a href="http://site.com/data/20">20</a>
<a href="http://site.com/data/21">21</a>
<a href="http://site.com/data/22">22</a>
<a href="http://site.com/data/23">23</a>
</span>
</div>
...more divs
</div
输出:
[{:"Data 1:"=>
["http://www.site.com/data/1",
"http://www.site.com/data/2"]},
...
{:"Data 20 :"=>
["http://www.site.com/data/20",
"http://www.site.com/data/21",
"http://www.site.com/data/22",
"http://www.site.com/data/20",]},
...
}]
一切都很顺利,正是我想要的
但是如果更改这些代码行:
detail = {}
[
[row.children.first.element_children,row.children.first.element_children],
].each do |part, link|
致:
我得到了
[{:"Data 1:"=>
["http://www.site.com/data/1"]},
...
{:"Data 20 :"=>
["http://www.site.com/data/20"]},
...
}]
阵列中仅存储第一个锚点href
我只需要澄清一下为什么它会这样,因为参数列表中的参数part
没有被使用,我想我不需要它。但是如果我同时删除相应的行.children.first.element\u children
,我的程序将无法正常工作
[[obj,obj],]中发生了什么。每个do
块?我一周前刚开始使用ruby,现在我还在习惯它的语法,如果有任何帮助,我将不胜感激。谢谢:D
编辑
行[0].children.first.element\u children[0]
将有输出
Nokogiri::XML::Element:0xcea69c name="a" attributes=[#<Nokogiri::XML::Attr:0xcea648
name="href" value="http://www.site.com/data/1">] children[<Nokogiri::XML::Text:0xcea1a4
"1">]>
您使代码过于复杂。查看您的代码,似乎您正在尝试获得以下内容:
require 'nokogiri'
doc = Nokogiri::HTML::Document.parse <<-eotl
<div>
<div class="NormalMid">
<span class="style-span">
"Data 1:"
<a href="http://site.com/data/1">1</a>
<a href="http://site.com/data/2">2</a>
</span>
</div>
<div class="NormalMid">
<span class="style-span">
"Data 20:"
<a href="http://site.com/data/20">20</a>
<a href="http://site.com/data/21">21</a>
<a href="http://site.com/data/22">22</a>
<a href="http://site.com/data/23">23</a>
</span>
</div>
</div
eotl
rows = doc.xpath("//div[@class='NormalMid']/span[@class='style-span']")
val = rows.map do |row|
[row.at_xpath("./text()").to_s.tr('"','').strip,row.xpath(".//@href").map(&:to_s)]
end
Hash[val]
# => {"Data 1:"=>["http://site.com/data/1", "http://site.com/data/2"],
# "Data 20:"=>
# ["http://site.com/data/20",
# "http://site.com/data/21",
# "http://site.com/data/22",
# "http://site.com/data/23"]}
你能提供
row.children.first.element\u children
的内容(实际的或合成的)吗?你写了一个简单的代码,用了一种更复杂的方式。哦,我的回答没有太多混乱:S@Bala-我编辑了我的帖子,向你展示了ArupRakshit的输出结果-耶,我觉得我做事情的方式太复杂了。由于我是Ruby新手,我不知道那么多的习惯用法,当我看到这个结构时,我认为它是“Ruby”的做事方式。谢谢你的澄清。我发现了irb,现在我可以测试我不懂的东西,我想你在帖子底部的解释帮助我理解了发生的事情:D
Nokogiri::XML::Element:0xcea69c name="a" attributes=[#<Nokogiri::XML::Attr:0xcea648
name="href" value="http://www.site.com/data/1">] children[<Nokogiri::XML::Text:0xcea1a4
"1">]>
<a href="http://www.site.com/data/1">1</a>
require 'nokogiri'
doc = Nokogiri::HTML::Document.parse <<-eotl
<div>
<div class="NormalMid">
<span class="style-span">
"Data 1:"
<a href="http://site.com/data/1">1</a>
<a href="http://site.com/data/2">2</a>
</span>
</div>
<div class="NormalMid">
<span class="style-span">
"Data 20:"
<a href="http://site.com/data/20">20</a>
<a href="http://site.com/data/21">21</a>
<a href="http://site.com/data/22">22</a>
<a href="http://site.com/data/23">23</a>
</span>
</div>
</div
eotl
rows = doc.xpath("//div[@class='NormalMid']/span[@class='style-span']")
val = rows.map do |row|
[row.at_xpath("./text()").to_s.tr('"','').strip,row.xpath(".//@href").map(&:to_s)]
end
Hash[val]
# => {"Data 1:"=>["http://site.com/data/1", "http://site.com/data/2"],
# "Data 20:"=>
# ["http://site.com/data/20",
# "http://site.com/data/21",
# "http://site.com/data/22",
# "http://site.com/data/23"]}
[[1],[4,5]].each do |a|
p a
end
# >> [1]
# >> [4, 5]
[[1,2],[4,5]].each do |a,b|
p a, b
end
# >> 1
# >> 2
# >> 4
# >> 5