2017-05-06 207 views
1

您好我想提取的每一个节点的数据,但我不知道该怎么做,真的感激,如果有人给我一些指导GOUTTE提取数据

<table> 
    <tr> 
     <td>item1</td> 
     <td>item2</td> 
    </tr> 
    <tr> 
     <td>item3</td> 
     <td>item4</td> 
    </tr> 
</table> 

这里是我的PHP代码:

$client = new Client(); 
    $crawler = $client->request('GET', 'https://www.socom'); 

    $crawler->filter('.tr')->each(function ($node) { 
     print $node->filter('.td')->text()."\n"; 
    }); 

回答

1

你以正确的方式,只是你指的是具有类tr和我在你的HTML见过你有没有你的HTML标签,所以,这就是为什么你不没有“成功”。

检查这一点,你可以访问你的tr元素中的每一个,并获得这种方式中的文本:

$crawler->filter('tr')->each(function($node) { 
    print_r($node->text()); 
}); 

注意输出是node,所以你不能使用echo,还有我只使用tr来引用元素。

,你也可以做到这一点,那就是更多的似乎也许你想要得到什么:

$crawler->filter('tr')->each(function($node) { 
    $node->filter('td')->each(function($nested_node) { 
    echo $nested_node->text() . "\n"; 
    }); 
}); 

这是让所有的获得tr在每trtd然后td对这些元素获取文本内。

就是这样,这是代码。

<?php 

require __DIR__ . '/vendor/autoload.php'; 

use Goutte\Client; 

$client = new Client(); 

$crawler = $client->request('GET', 'your_url'); 

$crawler->filter('tr')->each(function($node) { 
    print_r($node->text()); 
}); 

$crawler->filter('tr')->each(function($node) { 
    $node->filter('td')->each(function($nested_node) { 
    echo $nested_node->text() . "\n"; 
    }); 
}); 

希望它有帮助。