使用Goutte和PHP抓取列表以获取href的问题


Issue with scraping a list to get href using Goutte and PHP

我正在尝试抓取以下内容,我基本上想要文本和链接,我正在使用带有PHP的Goutte。我可以使用以下代码很好地获取文本,但我无法获取 href 值。任何帮助都会很棒。

$crawler->filter('#most-popular > div > ol > li > a')->each(function ($node) {
    var_dump($node->getAttribute('href'));
});

<li class="first-child ol1">
  <a href="http://www.bbc.co.uk/news/uk-england-south-yorkshire-31895703" class="story">
    <span class="livestats-icon livestats-1">1: </span>MP claims £17 poppy wreath expenses</a>
</li>

getAttribute()Crawler类中实现为attr()

$crawler->filter('#most-popular > div.panel.open > ol > li.first-child.ol1 > a')->each(function ($node) {
    var_dump($node->attr('href'));
});

下面的代码将解决此问题。

$crawler->filter('#most-popular > div.panel.open > ol > li.first-child.ol1 > a')->each(function ($node) {
    $href = $node->extract(array('href'));
    var_dump($href[0]);
});