Nokogiri Xpath 双循环
我想做的是 pul 包含 td 和类默认值的代码块。这工作得很好。但随后我需要整理代码块的不同部分。当我尝试使用第二个 xpath 调用执行此操作时,它每次都会打印每个块中的所有comheads
def HeaderProcessor(doc)
doc.xpath("//td[@class='default']").each do |block|
puts block.xpath("//span[@class='comhead']").text
end
end
当我只打印出块时,每个块都会打印一次并包含注释标题和注释。当我尝试运行 xpath 时,它会打印出 doc 中找到的每个comhead,并且似乎忽略了块变量。
关于如何使这项工作有效的任何想法?我对 xpath 有什么误解?
更新:
<td class="default">
<div style="margin-top:2px; margin-bottom:-10px; ">
<span class="comhead">
#some data
</span></div>
<br><span class="comment"><font color="#000000">#some more data</span>
</td>
What I'm trying to do is pul the code block that contains the td with the class default. This works perfectly fine. But then I need to sort out the different parts of the code block. When I try to do this with the second xpath call what it does is each time it prints all the comheads in each of the blocks
def HeaderProcessor(doc)
doc.xpath("//td[@class='default']").each do |block|
puts block.xpath("//span[@class='comhead']").text
end
end
When I just print out block each block prints out once and contains the comment header and the comment. When I try to run the xpath it prints out EVERY comhead found in doc and seems to be ignoring the block variable.
Any ideas on how I can make this work? What am I miss understanding about xpath?
UPDATE:
<td class="default">
<div style="margin-top:2px; margin-bottom:-10px; ">
<span class="comhead">
#some data
</span></div>
<br><span class="comment"><font color="#000000">#some more data</span>
</td>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您说
//span[@class='comhead']
时,您是在告诉 Nokogiri 从根开始搜索,您只需要*/span[@class='comhead']< /code>:
或者甚至只是这样:
如果您不需要对
元素执行任何操作。
You're telling Nokogiri to search from the root when you say
//span[@class='comhead']
, you just want*/span[@class='comhead']
:or even just this:
if you don't need to do anything with the
<td>
elements.