I am trying to retrieve all the images from this URL http://www.homegate.ch/kaufen/105652197?3
. I am using Xpaths in PHP. For some reason I can retrieve the body with Xpath but not the images. Here is my script:
<?php
$url = "http://www.homegate.ch/kaufen/105652197?3";
$body = '//body';
$img = '//img';
$html = file_get_contents($url);
# Call htmlentities as the $url content is not well-formatted: http://stackoverflow.com/questions/1685277/warning-domdocumentloadhtml-htmlparseentityref-expecting-in-entity
$html = htmlentities($html);
$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DomXPath($dom);
$query = $xpath->query($body);
if($query->length == 1)
echo $query->item(0)->nodeValue;
if($query->length < 1)
echo "Xpath for body is no good!";
$query = $xpath->query($img);
if($query->length == 1)
echo $query->item(0)->nodeValue;
if($query->length < 1)
echo "Xpath for image is no good!";
Running this script returns:
1. <!DOCTYPE html>..
2. Xpath for image is no good!
What is going wrong here? - Why is the Xpath only working on body
and not on img