gpt4 book ai didi

python - Xpath 选择
标签后的数据

转载 作者:行者123 更新时间:2023-11-30 22:15:19 25 4
gpt4 key购买 nike

我有这个html:

<div class="sys_key-facts">
<p>
<strong>Full-time </strong>1 year<br>
<strong>Part-time</strong> 2 years
</p>
</div>

我想获取Part-time(After Part-time)的值,即:

2 years</p>

我的 Xpath 代码是:

//div[@class="sys_key-facts"]/p[strong[contains(text(), "Part-time")]][preceding-sibling::br]/text() 

但这返回空。请让我知道我错在哪里。谢谢

最佳答案

问题是 p[preceding-sibling::br]表示该段落有换行兄弟,而 br实际上是p的 child - 不是 sibling

因此您可以将 XPath 更新为

//div[@class="sys_key-facts"]/p[strong[contains(text(), "Part-time")] and br]/text()[last()]

或者尝试下面的 XPath 来获取所需的输出:

//div[@class="sys_key-facts"]//strong[.="Part-time"]/following-sibling::text()

关于python - Xpath 选择 <br/> 标签后的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50328482/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com