【问题标题】:DomCrawler get element contents after specific elementDomCrawler 在特定元素之后获取元素内容
【发布时间】:2019-03-05 14:22:51
【问题描述】:

我正在尝试获取紧跟在另一个元素之后的元素内容。下面是一些示例代码:

<header>2010</header>
<div>
    <a href="">Some data</a>
    <a href="">Some data</a>
</div>
<header>2011</header>
<div>
    <a href="">Some data</a>
    <a href="">Some data</a>
</div>

我需要按年份对数据进行排序,我已经尝试了一些方法,但是对于 2010 年,它需要所有年份的数据。

$crawler->filter('header')->each(function(Crawler $c) {
$year = $c->text();
$next = $c->nextAll();
$next->filter('div a')->each(function($node){
    $node->text();
});
});

在两个标头之间获取所有div as 后如何让它停止?

【问题讨论】:

    标签: php laravel web-scraping domcrawler


    【解决方案1】:

    在您的情况下,您只能从 nextAll 中获取 first 节点,并且由于第一项是 div,因此仅在其中过滤 a

    $crawler->filter('header')->each(function(Crawler $c) {
        $year = $c->text();
        dump($year);
        $next = $c->nextAll()->first();
        $next->filter('a')->each(function($node){
            dump($node->text());
        });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-04
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多