I'm using PHP and xPath to crawl into a website I own (just crawl the html not going into the server) but I get this error:
Catchable fatal error: Object of class DOMNodeList could not be converted to string in C:\wamp\www\crawler.php on line 46
I already tried echoing just that line to see what I was getting but I would just get the same error also I tried googling for the error but I, in the end, ended up in the php documentation and found out my example is exactly as the one in php documentation except I'm working with an HTML instead of a XML...so I have no idea what's wrong...here's my code...
<?php
$html = file_get_contents('http://miurl.com/mipagina#0');
// create document object model
$dom = new DOMDocument();
// load html into document object model
@$dom->loadHTML($html);
// create domxpath instance
$xPath = new DOMXPath($dom);
// get all elements with a particular id and then loop through and print the href attribute
$elements = $xPath->query("//*[@class='nombrecomplejo']");
if ($elements != null) {
foreach ($elements as $e) {
echo parse_str($e);
}
}
?>
Edit
Actually yes sorry that line was to test when I had commented other stuff...I deleted it here still have the error though.
According to the documentation, the "$elements != null
" check is unnecessary. DOMXPath::query()
will always return a DOMNodeList
, though maybe it will be of zero length, which won't confuse the foreach
loop.
Also, note the use of the nodeValue
property to get the element's textual representation:
$elements = $xPath->query("//*[@class='nombrecomplejo']");
foreach ($elements as $e) {
echo $e->nodeValue;
}
The reason for the error you got is that you can't feed anything other than a string to parse_str()
, you tried passing in a DOMElement
.
Just a wild guess, but echo $elements; is line 46, right? I believe the echo command expects something that is a string or convertible to a string, which $elements is not. Try removing that line.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With