繁体   English   中英

XPATH检查节点内的特定文本

[英]XPATH to check on a specific text within a node

我将此作为要解析的节点:

<h3 class="atag">
    <a href="http://www.example.com">
      <span class="btag">text to be ignored</span>
         </a>
           <span class="ctag">text to be checked</span>
</h3>

我需要提取“ http://www.example.com ”,而不是要忽略的部分文本; 我还需要检查ctag是否包含要检查的文本。

我想出了这一点,但似乎没有完成。

response.xpath("//h3/a/@*[not(self::span)]").extract()

有什么想法吗?

如果您只需要从'a'标签中选择href,请使用@href。 要检查ctag是否包含一些文本,我想您可以使用如下代码:

'//h3[contains(span[@class="ctag"]/text(), "text to be checked")]/a/@href'

这将检查在给定的h3块内是否存在带有“待检查文本”的跨度。 如果文本存在,则将找到“ www.example.com”,否则结果为空。

您的意思是这样的XPath吗?

//h3/a[following-sibling::span[@class='ctag' and .='text to be checked']/@href

在XPath上方获取<a>标记,后跟<span class="ctag">包含"text to be checked" <span class="ctag">的值,然后从前面提到的<a>标记返回href属性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM