2016-04-02 34 views
2

我想從這個urlGoutte 的數據但是當我試圖過濾只有類「empresa」的div,我得到整個頁面。 如何過濾只有特定類的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> 

回答

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)