Ryan D
Ryan D

Reputation: 751

PHP Web Scrape with PHP Simple HTML DOM Parser

I am trying to get a data field using PHP Simple HTML DOM Parser. I can pull the links, images etc but cannot get a certain data attribute.

Example HTML -

<div id="used">
    <div id="srpVehicle-1C3CCCEG2FN601809" class="vehicle" data-vin="1C3CCCEG2FN601809">
    <div id="srpVehicle-1C3CCCEG2FN601810" class="vehicle" data-vin="1f2CfCEG2FN266778">
</div>

I would like to get all the "data-vin" fields on a site.

Here is my go at it -

$html = file_get_html($url);

foreach($html->find("div[data-vin]", 0) as $vin){
  echo $vin."<br>";
}

But it returns the whole page when I echo $vin. How can I access that data-vin field?

Upvotes: 0

Views: 519

Answers (1)

Stephen Clouse
Stephen Clouse

Reputation: 401

$html->find("data-vin", 0)

is looking for tags named data-vin, when you really want tags with the attribute data-vin.

foreach($html->find("[data-vin]") as $tag){
    echo $tag->getAttribute('data-vin')."<br>";
}

Upvotes: 1

Related Questions