Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't PHP XPath find table elements even though Firefox shows they exist?

Tags:

dom

php

xpath

I am trying to pull an exact table during a "web scrape." Used cURL to pull page into $html, which succeeds fine.

Used Firebug to get exact XPATH to the table needed.

Code follows:

$dom = new DOMDocument($html);
$dom->loadHTML($html);

$xpath = new DOMXpath($dom);
$summary = $xpath->evaluate('/html/body/table[5]/tbody/tr/td[3]/table/tbody/tr[8]/td/table');
echo "Summary Length: " . $summary->length;

When executed, $summary->length is always zero. It doesn't pull that table node.

Any ideas?


1 Answers

Firefox is liable to insert "virtual" tbody elements into tables that don't have them; do those elements exist in the original file?

like image 128
Rob Kennedy Avatar answered Mar 09 '26 22:03

Rob Kennedy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!