webscrapinhg a webite filtering for divs with a certain classname. How to do that?

Viewed 33

currently I´m tring to webscrape a site for football matches and I need to find out how to filter for divs with a specific name. Here is the code I already have. Thanks

include('simple_html_dom.php');
        
        $day = 1; //temporär

        $html = file_get_html('https://sport.sky.de/bundesliga-spielplan-ergebnisse-'.$day);

        $list = $html -> find('div[class="sdc-site-fixres__match-cell sdc-site-fixres__match-cell--score"]', 0);

        $list_array = $list -> find('div'); 

        for($i = 0; $i < sizeof($list_array); $i++){          
            echo $list_array[$i]->plaintext;
            echo "<br>";
        }
1 Answers

You can use xpath. Here is the full documentation.

$day = 1; //temporär

$html = file_get_contents('https://sport.sky.de/bundesliga-spielplan-ergebnisse-'.$day);
$doc = DOMDocument::loadHTML($html);
$xpath = new DOMXPath($doc);
$query = $xpath->query('//div[@class="sdc-site-fixres__match-cell sdc-site-fixres__match-cell--score"]/div/span[2]');

foreach ($query as $item) {
    /** @var DOMElement $item */
    echo $item->nodeValue;
    echo PHP_EOL;
}

Or you can benefit from symfony components for this purpose like DOM crawler or CSS selector

Related