我正在尝试从以下短语中提取国家(此处为印度尼西亚):
<small class="text-muted">
<span class="hidden-xs">Football / </span>Indonesia /
<span class="hidden-xs xh-highlight">Kick off: </span>11 Sep 2019, 11:30 </small>
目前,我仅使用以下命令提取文本:
.xpath('.//small[@class="text-muted"]/text()').extract()
提取印尼的正确正则表达式命令是什么?
您可以使用以下XPath-1.0表达式:
//small[@class="text-muted"]/span[@class="hidden-xs"]/following-sibling::text()
结果Indonesia /
。
如果要摆脱斜线,则有以下几种可能性:
从表达式中删除所有斜杠:
normalize-space(translate(//small[@class="text-muted"]/span[@class="hidden-xs"]/following-sibling::text(),"/",""))
使用substring-before()
得到斜线前的字符串:
normalize-space(substring-before(//small[@class="text-muted"]/span[@class="hidden-xs"]/following-sibling::text(),"/"))
使用substring-before()
来获取第一个空格前的字符串:
normalize-space(substring-before(//small[@class="text-muted"]/span[@class="hidden-xs"]/following-sibling::text()," "))
还有其他XPath表达式也可以使用。选择最适合您情况的一种。//
仅当您指定到当前节点的相对路径时,才有必要使用前导点。在上面的表达式中,我确实假定查找是全局的。
当然,这些XPath表达式必须被包围
.xpath('...').extract()
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句