如何在一组匹配的元素xpath中选择元素

问题描述 投票:0回答:1

我有以下html文本:

<div>
<p>
 "Text one"
  <br>
 "Text two"
  <br>
 "Text three"
  <br>
 "Text four"
</p>
</div>

我只需要使用xpath选择“文本2”和“文本3”。到目前为止,我有以下xpath可以选择整个文本:

 text = doc.xpath('/div/p/text()')

尽管我希望结果是:

 Text two
 Text three

我该如何实现?

python-3.x xpath
1个回答
0
投票

假设树中文本的位置决定了目标,请尝试将xpath表达式更改为

//div/p/text()[position()> 1 and position() < 4]
© www.soinside.com 2019 - 2024. All rights reserved.