【发布时间】:2016-09-18 23:14:44
【问题描述】:
我正在使用 XPath 来废弃一个网页,但我在代码的一部分上遇到了问题:
<div class="description">
here's the page description
<span> some other text</span>
<span> another tag </span>
</div>
我正在使用此代码从元素中获取值:
description = tree.xpath('//div[@class="description"]/text()')
我可以找到我正在寻找的正确 div,但我只想获取文本“这是页面描述”而不是内部跨度标签中的内容
任何人都知道我怎样才能只获取根节点中的文本而不获取子节点中的内容?
【问题讨论】:
-
xpath 表达式不应该包含 spans 的内容,只包含作为 div 的直接子节点的文本节点的内容:
["\n here's the page description\n ", '\n ', '\n']
标签: python xpath web-scraping