0

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&nbsp;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&nbsp;Strictly Come Dancing professional Vincent...</p>
</article>

これが私のXPATHです:

$articleLinks   = $finder->query('article[contains(@class,"module_article")]//@href');

ご覧のとおり、両方をつかんでいhrefsます。私は最初のものだけが必要です。

4

2 に答える 2

1

次の 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-19T19:54:39.993 に答える
0

で最初のものを取得する<a>にはhref:

$finder->query('article[contains(@class,"module_article")]/a[1]/@href')
于 2013-09-19T20:05:21.683 に答える