Search code examples
phpdomxpath

Trouble scraping table with DOMXPath


I have a table I'm trying to scrape that looks like this:

<table id="thisTable">
    <tr>
        <td class="value1"></td>
        <td class="value2"></td>
        <td class="value3"></td>
        <td class="value4"></td>
    </tr>
    <tr>
        <td class="value5"></td>
        <td class="value6"></td>
    </tr>
</table>

and my DOMXPath that looks like this (so far):

$htmlDoc = new DomDocument();
@$htmlDoc->loadhtml($html);
$xpath = new DOMXPath($htmlDoc);

$nodelist = $xpath->query('//*[@id="thisTable"]');

foreach ($nodelist as $n){
    echo $n->nodeValue."\n";
}

This works, I get the values of the table, but how do I specify the class of a nodeValue? Ultimately, my goal is to build a new table from the td's content of value2, value4 and value5 in a single row.


Solution

  • $htmlDoc = new DomDocument();
    $htmlDoc->loadHTML($html);
    $xpath = new DOMXPath($htmlDoc);
    
    $nodelist = $xpath->query('//td');
    
    foreach ($nodelist as $n){
        echo $n->getAttribute("class")."\n";
    }
    

    Note: Use getAttribute property for getting values of class