我正在尝试用一些场景来学习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,或者我是否需要编写一个方法来遍历元素,它的父元素然后是孩子?
请建议直接的方式,如果还有其他我会编写我自己的代码来遍历。
答案 0 :(得分:1)
如果需要根据子节点获取节点,则必须使用xpath-selector而不是css-selector。
要获取包含span的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>