Search code examples
phpxpathdomxpath

DOMXPath query error , shows nothing


I am using this code to get some metal name(e.g:copper,aluminium) from this site : http://www.kitcometals.com/ . this names are located at the left side of the page in a table named live spot prices.

here is my code :

<?php
$url = "http://www.kitcometals.com/";
$html = file_get_contents($url);
$dom = new DOMDocument();
$dom->loadHTML($html);
$xp = new DOMXPath($dom);

$qs = $xp->query("//table[@dwcopytype='CopyTableRow']/tbody/tr[@class='spot']/td[@class='menuB']");
foreach($qs as $q)
{
    echo $q->textContent."<br>";
}
?>

I have tested this XPath in google chrome's Xpath app . It has just worked fine showing every metals name in that table.showing this:

Copper Nickel Aluminum Zinc Lead Uranium

But it's not working in PHP code.Would anybody please figure out the problem ??


Solution

  • You have tested the xpath query against the "runtime" dom of the browser. But there is no tbody element in the actual document. Try

    //table[@dwcopytype='CopyTableRow']/tr[@class='spot']/td[@class='menuB']