我正在尝试用一些场景学习Nokogiri,现在正在使用它解析html代码。
doc = Nokogiri::HTML($driver.page_source)
在我的代码中,我有多个div部分,其中有一些嵌套的span和div标记,如下所示:
<div class="class1 class2 class3 class4" style="padding: 4px;" id="_28f331c5-3e42-4944-aa3f-15b68f453a2b">
<span class="class5" style="font-weight: bold; text-decoration: underline;">This is a String Text</span>
<div class="class6" dd:contenttype="content_type1" dd:concept="concept1" id="_8c83d6b3-55a1-4e88-b8af-9578bbfdef83">
<div class="class7">
<div class="class8" dd:contenttype="content_type2" dd:entityid="0" dd:entityversion="0" id="_cb48e502-9d87-451c-a7cf-4df104f21e51">
<div class="class9 class10" dd:btnfloatingstyle="top-right" dd:entitytexttype="resultval" id="_9e48a58b-3f06-4ce1-8a1e-fb7f4c5d1fc0" contenteditable="true"></div>
</div>
</div>
</div>
</div>
<div class="class1 class2 class3 class4" style="padding: 4px;" id="_28f331c5-3e42-4944-aa3f-15b68f453a2b">
<span class="class5" style="font-weight: bold; text-decoration: underline;">New String Text</span>
<div class="class6" dd:contenttype="content_type1" dd:concept="concept1" id="_8c83d6b3-55a1-4e88-b8af-9578bbfdef83">
<div class="class7">
<div class="class8" dd:contenttype="content_type2" dd:entityid="0" dd:entityversion="0" id="_cb48e502-9d87-451c-a7cf-4df104f21e51">
<div class="class9 class10" dd:btnfloatingstyle="top-right" dd:entitytexttype="resultval" id="_9e48a58b-3f06-4ce1-8a1e-fb7f4c5d1fc0" contenteditable="true"></div>
</div>
</div>
</div>
</div>
我想要的是,当我将字符串"This is a string Text"作为输入传递给一个方法时,它应该查看多个div部分,并向我返回相应的整个div部分及其嵌套的span和div。
有没有一种直接的方法可以使用Nokogiri或任何其他API实现我想要的目标,或者我需要编写一个方法来遍历元素、其父元素,然后遍历子元素??
请建议直接的方式,如果还有其他方式,我将编写自己的代码来遍历。
如果需要基于其子节点获取节点,则必须使用xpath选择器而不是css选择器。
要获得包含跨度的div,请执行:
node = doc.at_xpath('//div[./span[text()="New String Text"]]')
可运行的示例:
require 'nokogiri'
html = %q[
<body>
<div class="class1 class2 class3 class4" style="padding: 4px;" id="_28f331c5-3e42-4944-aa3f-15b68f453a2b">
<span class="class5" style="font-weight: bold; text-decoration: underline;">This is a String Text</span>
<div class="class6" dd:contenttype="content_type1" dd:concept="concept1" id="_8c83d6b3-55a1-4e88-b8af-9578bbfdef83">
<div class="class7">
<div class="class8" dd:contenttype="content_type2" dd:entityid="0" dd:entityversion="0" id="_cb48e502-9d87-451c-a7cf-4df104f21e51">
<div class="class9 class10" dd:btnfloatingstyle="top-right" dd:entitytexttype="resultval" id="_9e48a58b-3f06-4ce1-8a1e-fb7f4c5d1fc0" contenteditable="true"></div>
</div>
</div>
</div>
</div>
<div class="class1 class2 class3 class4" style="padding: 4px;" id="_28f331c5-3e42-4944-aa3f-15b68f453a2b">
<span class="class5" style="font-weight: bold; text-decoration: underline;">New String Text</span>
<div class="class6" dd:contenttype="content_type1" dd:concept="concept1" id="_8c83d6b3-55a1-4e88-b8af-9578bbfdef83">
<div class="class7">
<div class="class8" dd:contenttype="content_type2" dd:entityid="0" dd:entityversion="0" id="_cb48e502-9d87-451c-a7cf-4df104f21e51">
<div class="class9 class10" dd:btnfloatingstyle="top-right" dd:entitytexttype="resultval" id="_9e48a58b-3f06-4ce1-8a1e-fb7f4c5d1fc0" contenteditable="true">asdf</div>
</div>
</div>
</div>
</div>
</body>
]
doc = Nokogiri::HTML.parse(html)
puts doc.at_xpath('//div[./span[text()="New String Text"]]').to_html
#=> <div class="class1 class2 class3 class4" style="padding: 4px;" id="_28f331c5-3e42-4944-aa3f-15b68f453a2b">
#=> <span class="class5" style="font-weight: bold; text-decoration: underline;">New String Text</span>
#=> <div class="class6" dd:contenttype="content_type1" dd:concept="concept1" id="_8c83d6b3-55a1-4e88-b8af-9578bbfdef83">
#=> <div class="class7">
#=> <div class="class8" dd:contenttype="content_type2" dd:entityid="0" dd:entityversion="0" id="_cb48e502-9d87-451c-a7cf-4df104f21e51">
#=> <div class="class9 class10" dd:btnfloatingstyle="top-right" dd:entitytexttype="resultval" id="_9e48a58b-3f06-4ce1-8a1e-fb7f4c5d1fc0" contenteditable="true">asdf</div>
#=> </div>
#=> </div>
#=> </div>
#=> </div>