Below is my code:
$xpath = new DOMXPath($doc);
// Start from the root element
$query = '//div[contains(@class, "hudpagepad")]/div/ul/li/a';
$nodeList = @$xpath->query($query);
// The size is 104
$size = $nodeList->length;
for ( $i = 1; $i <= $size; $i++ ) {
$node = $nodeList->item($i-1);
$url = $node->getAttribute("href");
$error = scrapeURL($url);
}
function scrapeURL($url) {
$cfm = new DOMDocument();
$cfm->loadHTMLFile($url);
$cfmpath = new DOMXPath($cfm);
$pointer = $cfm->getElementById('content-area');
$filter = 'table/tr';
// The problem lies here
$state = $pointer->firstChild->nextSibling->nextSibling->nodeValue;
$nodeList = $cfmpath->query($filter, $pointer);
}
Basically this traverses to a list of links and scrapes each link with the scrapeURL method.
I don't know the problem here but randomly i get an non-object type error trying to get the $pointer
and sometimes it passes through without any error and the values are correct.
Anyone knows the problem here? I'm guessing that the point when the problem occurs is when the page is not loaded properly?