Jump to content

Grabbing all images using xpath


ukscotth

Recommended Posts

Hi,

 

i have this piece of code which scrapes data from a website and it works great. I want to also grab all the additional images that are in the div but I'm not very good with arrays etc and I'm a bit stuck.

 

Any ideas ?

 

Thanks in advance,

 

Scott

 

GrabHTML($car_link);

$links = $xpath->query( "//div[@id='contents']" ); 

$return = array();

foreach ( $links as $item ) {
	$newDom = new DOMDocument;
	$newDom->appendChild($newDom->importNode($item,true));

	$xpath = new DOMXPath( $newDom ); 
	$car_title = trim($xpath->query("//div[@class='rubrik']")
                   ->item(0)->nodeValue);
	$car_year = trim($xpath
                   ->query("//div[@class='infoRow'][1]")
                   ->item(0)->nodeValue);
	$car_color = trim($xpath
                   ->query("//div[@class='infoRowAlternate'][1]")
                   ->item(0)->nodeValue);
	$car_milage = trim($xpath
                   ->query("//div[@class='infoRow'][2]")
                   ->item(0)->nodeValue);
	$car_price = trim($xpath
                   ->query("//div[@class='infoRowAlternate'][2]")
                   ->item(0)->nodeValue);
	$car_seller = trim($xpath
                   ->query("//div[@class='infoRow'][3]")
                   ->item(0)->nodeValue);
	$car_desc = trim($xpath
                   ->query("//div[@class='infoRowAlternate'][3]")
                   ->item(0)->nodeValue);
	$car_desc2 = trim($xpath
                   ->query("//div[@class='infoRow'][4]")
                   ->item(0)->nodeValue);
	$car_image = trim($xpath
                   ->query("//img[1]/@src")
                   ->item(0)->nodeValue);

}

Link to comment
https://forums.phpfreaks.com/topic/241606-grabbing-all-images-using-xpath/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.