我正在学习,但我需要在下面的“教育”标题下面的段落文本中获取XPath或正则表达式。
当使用XPath时,问题是,关于“Education”标题的所有内容可能都有不可预测的元素,段落等。也就是说,“Education”下面的内容可能并不总是位于div /段位置X但它总是低于“教育”标题。
非常感谢。
<div class="toggle_cls">
<p>The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog.</p>
<div class="bold" style="margin-top:20px;">Previous Work Experience</div>
<p>The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog.</p>
<div class="bold" style="margin-top: 20px;"><span class="bold">Education</span></div>
<p>The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog.</p>
<p> </p>
</div><span class="text_more hidden noDisp">(Read Full Bio)</span><span class="text_less hidden noDisp">(Less)</span><span class="text_charlength hidden noDisp">275</span>
</div>
答案 0 :(得分:1)
查找计算文本的元素的XPath是“教育”并选择它的第一个跟随兄弟:
//*[.='Education']/following-sibling::*[1]
答案 1 :(得分:0)
使用jquery的解决方案,不是很可靠,但可以完成这项工作。
var education_tag= $('div:contains("Education")');
var p_tags= education_tag.nextAll('p');
console.log(p_tags);
var education_paragraph= '';
for( var i=0,len=p_tags.length;i<len;i++)
education_paragraph+= p_tags[i].innerText+ " ";
console.log(education_paragraph);
var result_div= document.createElement("div");
result_div.innerHTML= education_paragraph;
result_div.id= "education_paragraph";
document.body.appendChild(result_div);
&#13;
#education_paragraph{
background: rgb(200,200,200);
margin:2%;
padding:1%;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="content" style="display:none">
<div class="toggle_cls">
<p>The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog.</p>
<div class="bold" style="margin-top:20px;">Previous Work Experience</div>
<p>The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog.</p>
<div class="bold" style="margin-top: 20px;"><span class="bold">Education</span></div>
<p>The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog.</p>
<p> </p>
</div><span class="text_more hidden noDisp">(Read Full Bio)</span><span class="text_less hidden noDisp">(Less)</span><span class="text_charlength hidden noDisp">275</span>
</div>
&#13;