如何在XPath表达式中跳过带注释的段落?

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

我正在尝试使用以下Xpath表达式来抓取this之类的网站:

.//div[@class="tresc"]/p[not(starts-with(text(), "<!--"))]

事实是第一段是注释部分,所以我想跳过它:

<!--[if gte mso 9]><xml>
<w:WordDocument>
<w:View>Normal</w:View>
<w:Zoom>0</w:Zoom>
<w:HyphenationZone>21</w:HyphenationZone>
<w:PunctuationKerning />
<w:ValidateAgainstSchemas />
<w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid
<w:IgnoreMixedContent>false</w:IgnoreMixedContent
<w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
<w:Compatibility>
<w:BreakWrappedTables />
<w:SnapToGridInCell />
<w:WrapTextWithPunct />
<w:UseAsianBreakRules />
<w:DontGrowAutofit />
</w:Compatibility>
<w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel>
</w:WordDocument>
</xml><![endif]-->

[不幸的是,我的表达并没有跳过带注释的段落。有人知道我在做什么错吗?

xpath web-scraping scrapy xpath-2.0
1个回答
0
投票
注释不是text()的一部分,它们构成了自己的节点:comment()。要排除包含注释的p,请使用

p[not(comment())]

© www.soinside.com 2019 - 2024. All rights reserved.