【问题标题】:Issue with scraping a list to get href using Goutte and PHP使用 Goutte 和 PHP 抓取列表以获取 href 的问题
【发布时间】:2015-05-18 09:30:15
【问题描述】:

我正在尝试抓取以下内容,我基本上想要文本和链接,我正在使用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>

【问题讨论】:

  • 感谢编辑@halfer

标签: php web-scraping goutte


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    $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]);
    });
    

    【讨论】:

      猜你喜欢
      • 2018-10-23
      • 1970-01-01
      • 2018-10-17
      • 2015-11-14
      • 2019-12-23
      • 1970-01-01
      • 1970-01-01
      • 2016-06-23
      • 2015-05-20
      相关资源
      最近更新 更多