无法在 div 中获得所有 link

Can't get all link in a div

我正在尝试从这个 page 中获取所有 link。实际上,我几乎用这段代码实现了这个结果:

public function getLinks()
{
    $html = file_get_html("http://it.soccerway.com/national/italy/serie-a/20152016/regular-season/r31554/");

    foreach($html->find("div.block_competition_left_tree-wrapper") as $div)
    {
        foreach ($div->find('a') as $li)
        {
            echo $li->href . "<br>";
        }
    }

}

这是结果:

/national/italy/serie-a/c13/
/national/italy/serie-a/20152016/s11663/
/national/italy/serie-b/c14/
/national/italy/serie-c1/c53/
/national/italy/serie-c2/c358/
/national/italy/serie-d/c659/
/national/italy/coppa-italia/c135/
/national/italy/super-cup/c171/
/national/italy/coppa-italia-serie-c/c684/
/national/italy/campionato-nazionale-primavera/c952/
/national/italy/coppa-italia-primavera/c1070/
/national/italy/super-coppa-primavera/c1171/
/national/italy/dante-berretti/c1092/
/national/italy/serie-a-women/c293/
/national/italy/serie-a2/c457/
/national/italy/coppa-italia-women/c852/
/national/italy/super-cup-women/c851/
/national/italy/club-friendlies/

问题是我只需要抓取列表 <li> 中的 link,你怎么能在 html 中看到不同的 classes expanded | odd | even。本质上,我不希望将元素的 link 显示为 Serie A - Serie B, etc...,而是将 link 显示在其中。特别是这样的结果应该是:

/national/italy/serie-a/20152016/s11663/
/national/italy/serie-b/20152016/regular-season/r31798/
/national/italy/serie-c1/20152016/girone-c/r31861/

现在,如果您在上面的第一个结果中看到,在我的最后一个示例中只有 /national/italy/serie-a/20152016/s11663/ 是正确的,这是因为在 html 页面中 Serie A 项目具有 class expanded 代码见link。我怎样才能修复我的代码来实现这个目标?

希望,我也能理解你。你需要像你一样获取所有link,然后打开每个link以获取class的所有link。

一个例子:

public function getLinks()
{
    $html = file_get_html("http://it.soccerway.com/national/italy/serie-a/20152016/regular-season/r31554/");

    foreach($html->find("div.block_competition_left_tree-wrapper") as $div)
    {

        //get all links
        foreach ($div->find('a') as $li)
        {
            $openLink = file_get_html("http://it.soccerway.com/".$li->href);

            foreach($openLink->find("div.block_competition_left_tree-wrapper") as $divOfNewLink){

                foreach ($divOfNewLink->find('li') as $liOfNewDiv){

                        if (preg_match("/expanded/i", $liOfNewDiv->class)) {

                            foreach ($liOfNewDiv->find('a') as $link)
                            {
                                echo $link->href . "<br>";
                            }

                        }else{
                             // do nothing
                            }


                }
            }

        }


  }
}