gpt4 book ai didi

python - XPath 选择所有但不选择 self::strong 和 self::strong/following-sibling::text()

转载 作者:行者123 更新时间:2023-11-28 16:41:52 27 4
gpt4 key购买 nike

所以我有以下要解析的示例 html。

<div>
<strong>Title:</strong>
Sub Editor at NEWS ABC

<strong>Name:</strong>
John

<strong>Where:</strong>
Everywhere

<strong>When:</strong>
Anytime

<strong>Everything can go down there..</strong>

Lorem Ipsum blah blah blah....
</div>

我想提取整个 div,但我不希望 Title 以及 Where 和 When 的标题具有以下值。

到目前为止,我已经测试了以下 XPath。

a) 没有跟随兄弟(1:不工作。2:工作)

1. //div/node()[not(strong[contains(text(), "Title")])]

2. //div/node()[not(self::strong and contains(text(), "Title"))]

a) 与以下 sibling (1:不工作。2:不工作)

1. //div/node()[not(strong[contains(text(), "Title")]) and not(strong[contains(text(), "Title")]/following-sibling::text())]

2. //div/node()[not(self::strong and contains(text(), "Title") and following-sibling::text())]

如何实现我所追求的?

最佳答案

我认为以下符合您的要求 - 它排除了包含标题的强元素以及它后面的文本节点。您可以扩展它以包括您想要排除的其他强元素:

//div/node()[not(self::strong and contains(text(), "Title") or preceding-sibling::strong[1][contains(text(), "Title")])]

强节点被跳过:

not(self::strong and contains(text(), "Title")

以下文本被跳过:

preceding-sibling::strong[1][contains(text(), "Title")]

请注意,文本节点需要检查其最接近的前一个兄弟节点(而不是其后一个兄弟节点)。

关于python - XPath 选择所有但不选择 self::strong 和 self::strong/following-sibling::text(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18513128/

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