簡體   English   中英

Dom Xpath從html獲得第一個錨href

[英]Dom xpath get first anchor href from html

這是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 我只需要第一個。

使用此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

要使用href檢索第一個<a>

$finder->query('article[contains(@class,"module_article")]/a[1]/@href')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM