【问题标题】:DOM Crawler and The current node list is empty - how to get listDOM Crawler 和当前节点列表为空 - 如何获取列表
【发布时间】:2016-12-07 21:11:52
【问题描述】:

我需要有关 Symfony 3.2 中 DOM Crawler 的帮助。 这是我的代码:

$html = file_get_contents('http://www.wakacje.pl/wczasy/peru/');
$crawler = new Crawler($html);

$crawler = $crawler->filter('#gridWithPagination > div > div')->each(function (Crawler $node) {
    return $node->filter('div.desc > a');
});

foreach ($crawler as $item) {
    var_dump($item->attr('href'));
}

如您所见,我想从列表中的所有匹配项中获取 URI 参数,但此代码仅返回第一个实例,并且还返回错误:

当前节点列表为空

我错过了什么? 提前感谢您的帮助。

【问题讨论】:

    标签: symfony domcrawler


    【解决方案1】:

    不知道为什么需要过滤两次,但更直接的 CSS 选择器似乎对我有用:

    use Symfony\Component\DomCrawler\Crawler;
    
    $html = file_get_contents('http://www.wakacje.pl/wczasy/peru/');
    $crawler = new Crawler($html);
    
    $nodes = $crawler->filter('div#gridWithPagination div.desc a');
    
    $nodes->each(function ($item) {
        echo $item->attr('href').'<br>';
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-07-14
      • 2023-03-26
      • 1970-01-01
      • 2012-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多