XPath选择all但不是self :: strong和self :: strong / following-sibling :: text()

时间:2013-08-29 14:03:39

标签: python xpath

所以我有以下示例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)跟随兄弟姐妹(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())]

如何实现我的目标?

1 个答案:

答案 0 :(得分:3)

我认为以下内容符合您要做的事情 - 它排除了包含标题的强元素以及它之后的文本节点。您可以将其展开以包含要排除的其他强大元素:

//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")]

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