使用同一个类simplehtmldom只抓取一个锚


Scrape only one Anchor with same class simplehtmldom

我试图使用simplehtmldom只返回一个具有相同类名的锚,目前它返回页面中的所有锚,因为它们都具有相同的类名。

下面是我使用的:

foreach($html->find('a.db-link') as $link) {
echo $link->href . '<br>';
echo $link->plaintext . '<br>';
}

从上面我得到:

http://example.com
link 1
http://example.com/2
link 2
http://example.com/3
link 3

我尝试使用find元素:

echo $link->find('text', 0);

那没用。

我怎样才能得到第一个锚?

这应该可以工作,它经过测试:

include_once('simple_html_dom.php');
$html = str_get_html('
<a class="db-link" href="https://www.google.com/1">Google 1</a>
<a class="db-link" href="https://www.google.com/2">Google 2</a>
<a class="db-link" href="https://www.google.com/3">Google 3</a>
<a class="db-link" href="https://www.google.com/4">Google 4</a>
<a class="db-link" href="https://www.google.com/5">Google 5</a>
');

echo $html->find('a.db-link', 0);

结果:

谷歌1