简体   繁体   中英

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?

$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>";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM