无法获得div中的所有链接

时间:2016-03-12 15:26:53

标签: php html simple-html-dom scrape

我试图从page获取所有链接。实际上,我几乎用这段代码实现了这个结果:

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>中的链接,如何在html中看到有不同的类expanded | odd | even。基本上我不想让元素的链接显示为Serie A - Serie B, etc...,但链接里面的链接。特别是这样的结果应该是结果:

/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项具有类{{1并且代码看到了链接。如何修复我的代码来实现这一目标?

1 个答案:

答案 0 :(得分:3)

我希望,我也了解你。您需要像以前一样获取所有链接,然后打开每个链接以获取该类的所有链接。

一个例子:

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
                            }


                }
            }

        }


  }
}