0

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 ??

Quazi Marufur Rahman
  • 2,603
  • 5
  • 33
  • 51

1 Answers1

1

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']
VolkerK
  • 95,432
  • 20
  • 163
  • 226
  • your expression is same as mine – Quazi Marufur Rahman Nov 07 '11 at 15:44
  • code has worked fine after your help . would you explain why chromes 'inspect element' showing 'tbody' ?? after downloading the page I have checked the src in text editor and its also showing same result . but when I am checking the src in browser window there is no tbody element . Its really wired . How can I check the XPath efficiently ?? – Quazi Marufur Rahman Nov 07 '11 at 16:03
  • see http://stackoverflow.com/questions/938083/why-do-browsers-insert-tbody-element-into-table-elements . As for how to test it efficiently: That's another question for stackoverflow. One which I can't answer sufficiently ;-) – VolkerK Nov 07 '11 at 16:12