用Path刮一张桌子

I have a table in a html page that looks like this (pastebin url).

The current code I'm trying to grab the content from the table is:

$html = htmlspecialchars("https://localhost/table.php");

$doc = new \DOMDocument();

if($doc->loadHTML($html))
{
    $result = new \DOMDocument();
    $result->formatOutput = true;
    $table = $result->appendChild($result->createElement("table"));
    $thead = $table->appendChild($result->createElement("thead"));
    $tbody = $table->appendChild($result->createElement("tbody"));

    $xpath = new \DOMXPath($doc);

    $newRow = $thead->appendChild($result->createElement("tr"));

    foreach($xpath->query("//table[@id='kurstabell']/thead/tr/th[position()>1]") as $header)
    {
        $newRow->appendChild($result->createElement("th", trim($header->nodeValue)));
    }

    foreach($xpath->query("//table[@id='kurstabell']/tbody/tr") as $row)
    {
        $newRow = $tbody->appendChild($result->createElement("tr"));

        foreach($xpath->query("./td[position()>1]", $row) as $cell)
        {
            $newRow->appendChild($result->createElement("td", trim($cell->nodeValue)));
        }
    }

    echo $result->saveXML($result->documentElement);
}

print_r($result);

(Im using htmlspecialchars because libxml_use_internal_errors(true); generates error code Europe/Berlin] PHP Warning: DOMDocument::loadHTML(): htmlParseEntityRef: expecting ';' in Entity, line: So i read somewhere that htmlspecialchars ok to use)

The current result of this snipp looks like this:

DOMDocument Object ( [doctype] => [implementation] => (object value omitted) [documentElement] => (object value omitted) [actualEncoding] => [encoding] => [xmlEncoding] => [standalone] => 1 [xmlStandalone] => 1 [version] => 1.0 [xmlVersion] => 1.0 [strictErrorChecking] => 1 [documentURI] => [config] => [formatOutput] => 1 [validateOnParse] => [resolveExternals] => [preserveWhiteSpace] => 1 [recover] => [substituteEntities] => [nodeName] => #document [nodeValue] => [nodeType] => 9 [parentNode] => [childNodes] => (object value omitted) [firstChild] => (object value omitted) [lastChild] => (object value omitted) [previousSibling] => [attributes] => [ownerDocument] => [namespaceURI] => [prefix] => [localName] => [baseURI] => [textContent] => )

php_error.log doesn't give me any errors.

The expected result is the same table, echoed in html, but with all "unnecessary" code removed.

My question: What is wrong with the current piece of code?

The problem is with the first line:

$html = htmlspecialchars("https://localhost/table.php");

It should simply be:

$html = file_get_contents("https://localhost/table.php");

The function htmlspecialchars() escapes all HTML tags which, when parsed by loadHTML() returns a single text node rather than the expected DOM.