【发布时间】:2014-08-04 10:50:07
【问题描述】:
我正在尝试抓取具有产品列表的网站的一些文本。获取每个 div 中仅第一次出现的类标记的文本的 XPath 是什么?在下面的代码中,我需要每个 div "foo" 的 span "bar" 文本第一次出现。
所以我需要只给我“A 年”、“C 年”等的 XPath。
我是新手,不知道怎么做。非常感谢您提供的任何帮助!
<div class="foo">
<span class="bar">year A</span>
<span class="qux">some text</span>
<span class="bar">year B</span>
</div>
<div class="foo">
<span class="bar">year C</span>
<span class="qux">some text</span>
<span class="bar">year D</span>
</div>
Etc.
使用像 //span[@class='bar'][1]/text() 这样的东西只会得到“A 年”。
使用类似 //*[contains(@class, 'bar')]/text() 的内容,会得到“A 年”、“B 年”、“C 年”和“D 年”。
我正在抓取多个页面,每个页面上的项目数不同。类名“bar”只用于我需要的元素,所以这里描述的问题:What is the XPath expression to find only the first occurrence?不适用。
【问题讨论】: