I traversed a DOM in search for images in a certain div. Ive tested the xpath expression in Chrome and it works. Automating this in PHP is tedious, so far. When i use var_dump on the nodelist object, it seems something is inside the object: object(DOMNodeList)#11 (0) { } However, when i use nodeValue on the nodelist, i don’t get any results. My guess is because it are images, it cannot be displayed as a string, this is basically what nodeValue does. Anyone has experience with gathering images via xpath? How to retrieve the images that i’ve gathered?
$article = file_get_contents($url);
// get images from article via xpath
$html = new DOMDocument();
@$html->loadHtml($article);
$xpath = new DOMXPath($html);
$nodelist = $xpath->query('//div[@class="contentBody contentBody-collapsed morph"]//img');
var_dump($nodelist);
foreach ($nodelist as $n)
print $n->nodeValue."<br/>\n";
Try e.g.: