Reputation: 3
i am trying to make a simple scrape code for my site but i get and error when i run it
i got this error code
Notice: Trying to get property of non-object
this is my code
function get_web_page( $url )
{
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "spider", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_SSL_VERIFYPEER => false // Disabled SSL Cert checks
);
$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
$content = curl_exec( $ch );
$err = curl_errno( $ch );
$errmsg = curl_error( $ch );
$header = curl_getinfo( $ch );
curl_close( $ch );
$header['errno'] = $err;
$header['errmsg'] = $errmsg;
$header['content'] = $content;
return $header;
}
libxml_use_internal_errors(true);
$url = 'https://www.mourjan.com/ae/abu-dhabi/lands';
$amer=get_web_page($url);
$html= $amer['content'];
$dom = new DOMDocument();
@$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$dom = new DOMDocument;
$dom->loadHTML($html);
foreach ($dom->getElementsByTagName('p') as $td) {
if ($td->hasAttribute('onclick')) {
$links= $td->getAttribute('onclick');
$links = str_replace("wo('/","",$links);
$links = str_replace("/')","",$links);
$url = 'https://www.mourjan.com/ae/abu-dhabi/lands/'.$links.'';
$amer=get_web_page($url);
$html= $amer['content'];
$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$name = $xpath->query('*[@id="results"]/div[2]/div[2]/p');
$phone= $xpath->query('*[@id="results"]/div[2]/div[2]/p/span');
echo $name->item(0)->nodeValue;
echo '<br />';
echo str_replace(' ','',$phone->item(0)->nodeValue);
}
}
i hope that can someone help me in fixing this issue, i used this xpath because it is helpful better than the other
regards Amer
Upvotes: 0
Views: 641
Reputation: 19492
If the Xpath expression returns an empty node list then access ->item(0)
will return NULL
- hence the error message.
You could use ->length > 0
to validate that here is a node in the list before accessing the item.
Another possibility is using DOMXpath::evaluate()
and casting the node list into a string in the Xpath expression. An empty list will result in an empty string:
$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$name = $xpath->evaluate('string(*[@id="results"]/div[2]/div[2]/p)');
$phone = $xpath->evaluate('string(*[@id="results"]/div[2]/div[2]/p/span)');
echo $name;
echo '<br />';
echo str_replace(' ','',$phone);
Upvotes: 0
Reputation: 418
Maybe the query
$name = $xpath->query('*[@id="results"]/div[2]/div[2]/p');
has no result. Then you would make here
echo $name->item(0)->nodeValue;
a call on an empty nodelist (length($name) = 0). Ergo "item(0)" doesn't exist. So the property "nodeValue" cannot be requested.
Upvotes: 1