我正在尝试使用以下 Xpath 表达式抓取这样的网站:
.//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]-->
不幸的是,我的表达没有跳过带有评论的段落。有人知道我做错了什么吗?
注释不是text()
的一部分,它们构成了自己的节点:comment()
。要排除包含注释p
,请使用
p[not(comment())]