【问题标题】:Get div with class with Goutte使用 Goutte 获取 div
【发布时间】:2016-07-22 15:18:30
【问题描述】:

我正在尝试使用 Goutte 从此 url 获取数据 但是,当我尝试仅过滤具有“empresa”类的 div 时,我得到了整个页面。 如何仅过滤具有特定类的 div?

这是我的代码:

<html>

<body>
        <?php

        require __DIR__ . '/vendor/autoload.php';
        use Goutte\Client;

        $client = new Client();
        $crawler = $client->request('GET', 'http://sp.cadastrosindustriais.com.br/?consulta=cal%C3%A7ados');

        $crawler->filter('div[id="empresa"]')->each(function ($node) {
            print $node->text()."\n";
        });


        ?>

</body>


</html>

【问题讨论】:

    标签: php goutte


    【解决方案1】:

    你已经接近了。问题是你的选择器。 crawler 使用 jquery style selectors

    这是您的代码的一个工作示例。我将结果放在一个数组中,以防您想要做的不仅仅是转储结果。

    $client = new Goutte\Client();
    $crawler = $client->request('get', 'http://sp.cadastrosindustriais.com.br/?consulta=cal%C3%A7ados');
    
    $elements = $crawler->filter('.empresa')->each(function($node){
        return $node->text();
    });
    

    那么如果你想遍历结果,你可以做foreach($elements as $e)

    【讨论】:

      猜你喜欢
      • 2016-10-23
      • 1970-01-01
      • 2019-12-23
      • 2014-05-05
      • 2019-11-07
      • 2014-09-22
      • 1970-01-01
      • 2015-05-18
      • 2020-04-22
      相关资源
      最近更新 更多