4

我正在尝试抓取以下内容,我基本上想要文本和链接,我正在使用Goutte和 PHP。我可以使用以下代码很好地获取文本,但无法获取 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>
4

2 回答 2

18

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

$crawler->filter('#most-popular > div.panel.open > ol > li.first-child.ol1 > a')->each(function ($node) {
    var_dump($node->attr('href'));
});
于 2016-02-23T04:34:11.807 回答
10

下面的代码将解决这个问题。

$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]);
});
于 2015-03-16T10:01:22.353 回答