下面是HTML:
<article class="module_article featured">
<a title="Exclusive: Strictly's Vincent Simone welcomes baby boy" href="h/mother-and-baby/2013091914634/vincent-simone-baby-boy-born/"><h1 class="article_title">Exclusive: Strictly's Vincent Simone welcomes baby boy</h1></a> <a href="/healthandbeauty/mother-and-baby/2013091914634/vincent-simone-baby-boy-born/">
<img src="/imagenes/portadas/1-40-vincent-s.jpg">
</a>
<a href="/healthandbeauty/mother-and-baby/2013091914634/vincent-simone-baby-boy-born/">
<img src="/imagenes/portadas/1-40-vincent-s.jpg">
</a>
<p>HELLO! Online can exclusively reveal that Strictly Come Dancing professional Vincent...</p>
</article>
<article class="module_article featured">
<a title="Exclusive: Strictly's Vincent Simone welcomes baby boy" href="h/mother-and-baby/2013091914634/vincent-simone-baby-boy-born/"><h1 class="article_title">Exclusive: Strictly's Vincent Simone welcomes baby boy</h1></a> <a href="/healthandbeauty/mother-and-baby/2013091914634/vincent-simone-baby-boy-born/">
<img src="/imagenes/portadas/1-40-vincent-s.jpg">
</a>
<a href="/healthandbeauty/mother-and-baby/2013091914634/vincent-simone-baby-boy-born/">
<img src="/imagenes/portadas/1-40-vincent-s.jpg">
</a>
<p>HELLO! Online can exclusively reveal that Strictly Come Dancing professional Vincent...</p>
</article>下面是我的XPATH:
$articleLinks = $finder->query('article[contains(@class,"module_article")]//@href');正如你所看到的,它抓住了两个hrefs。我只需要第一个。
发布于 2013-09-19 19:54:39
使用这个XPATH表达式:
(/article[contains(@class,"module_article")]//@href)[1]输出:
h/mother-and-baby/2013091914634/vincent-simone-baby-boy-born/更新(根据上次编辑)
/article[contains(@class,"module_article")]/a[1]/@href演示示例:
<foo>
<a href='#1'>1</a>
<bar>
<a href='#2'>2</a>
</bar>
</foo>
<foo>
<a href='#3'>3</a>
<baz> <a href='#4'>4</a> </baz>
</foo>XPATH
/foo/a[1]/@href输出:
#1
#3发布于 2013-09-19 20:05:21
使用<a>检索第一个href
$finder->query('article[contains(@class,"module_article")]/a[1]/@href')https://stackoverflow.com/questions/18903682
复制相似问题