【问题标题】:Goutte extract data from every nodeGoutte 从每个节点中提取数据
【发布时间】:2017-10-04 23:54:24
【问题描述】:

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

<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";
    });

【问题讨论】:

    标签: goutte


    【解决方案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 上获取所有tr 获取它的td,然后在那些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";
      });
    });
    

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 2021-11-18
      • 2018-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-09
      • 2016-05-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多