python - Xpath 在第一个 html 标记后获取文本

标签 python html xpath

还有下一个区 block

<div class="text">
  <h1>head1</h1>
    Text1 <br/><br/> text12  <br/><br/> text 13
  <h1>head11</h1>
    Text11
  <h3>head3</h3>
    Text2
</div>

如何在第一个 H1 后使用忽略获取文本 <br/><br/>作为

Text1 
text12
text 13

我使用 Grab Python page = g.doc.select('//div[@class="text"]/h3[1]/following-sibling::text()]') 结果是

Text1
text12
text 13
Text11
Text2

最佳答案

您可以尝试选择只有一个 h1 sibling 的 text()...

//div[@class='text']/text()[count(preceding-sibling::h1)=1]

另一种选择是尝试使用 Kayessian 方法...

//div[@class='text']/h1[1]/following-sibling::text()[count(.|//div[@class='text']/h1[1+1]/preceding-sibling::text()) = count(//div[@class='text']/h1[1+1]/preceding-sibling::text())]

这是一个更好的 example and explanation of the Kayessian method .

关于python - Xpath 在第一个 html 标记后获取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45511841/

相关文章:

python转换为yaml格式问题

python - 如何加速随机梯度下降?

python - 将应用操作中的对象合并到 PANDAS 中的数据帧

javascript - HTML 元素的坐标

python - Selenium Python 按文本/样式单击页面上的链接

python - 交换两个字符串之间的单词但保留其他字符

javascript - 根据输赢情况在 div 中显示照片有自己的照片数组。同时使用 JS 和 HTML

html - CSS 类与 CSS id 冲突

python - 如何选择文档中的所有文本节点?

c# - 在具有相同属性的 XML 元素之间导航