XPath选择<div>的所有文本内容,但特定标记<h5>除外

bsl*_*ima 9 html xpath siblings

我搜索并尝试了几个解决这个问题的方法,但没有一个工作:我有这个HTML

<div class="detalhes_colunadados">
   <div class="detalhescolunadados_blocos">
     <h5>Descrição completa</h5>
    Sala de estar/jantar,2 vagas de garagem cobertas.<br>
    </div>
    <div class="detalhescolunadados_blocos">
      <h5>Valores</h5>
            Venda: R$ 600.000,00<br>
          Condomínio: R$ 660,00<br>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

并且想要通过XPath仅提取第一个div class ="detalhescolunadados_blocos"中不是h5标签的文本内容.

我试过:// div [@ class ='detalhescolunadados_blocos']/[1]/*[not(self :: h5)]

nwe*_*hof 12

尝试以下XPath表达式:

//div[@class='detalhescolunadados_blocos'][1]//text()[not(ancestor::h5)]
Run Code Online (Sandbox Code Playgroud)

这将返回:

$ xmllint --html --shell so.html
/ > xpath //div[@class='detalhescolunadados_blocos'][1]//text()[not(ancestor::h5)]    
Object is a Node Set :
Set contains 2 nodes:
1  TEXT
    content=      
2  TEXT
    content=     Sala de estar/jantar,2 vagas de gar...
Run Code Online (Sandbox Code Playgroud)

  • 为什么不使用`xmllint --html --xpath'// foo'file.html`?=) (2认同)