i'm trying to figure out how to get a attribute from a specific div on a web page, but my query isn't returning anything. I'm assuming my query is just wrong.
i'm trying to get the data-original url from this div:
<div
class="p-dynamic-image vox-lazy-load lazy-loaded”
data-original="https://cdn2.vox-cdn.com/thumbor/Bv7HhgyCscPLXrx2qHPzo8dlU4g=/0x0:7015x4677/2050x1367/cdn0.vox-cdn.com/uploads/chorus_image/image/46369294/RzrFirefly_01_DA.0.0.jpg”
style="background-image: url(https://cdn2.vox-cdn.com/thumbor/Bv7HhgyCscPLXrx2qHPzo8dlU4g=/0x0:7015x4677/2050x1367/cdn0.vox-cdn.com/uploads/chorus_image/image/46369294/RzrFirefly_01_DA.0.0.jpg);">
</div>
my code:
$xpath = new DOMXpath($page_dom);
$image = $xpath->query("//div[@class='p-dynamic-image vox-lazy-load lazy-loaded']/@data-original")->value;
echo $image . "<br />";
but this isn't returning anything.
what am I doing wrong?
In fact, it should be:
<?php
$html_string ='<div
class="p-dynamic-image vox-lazy-load lazy-loaded"
data-original="https://cdn2.vox-cdn.com/..." style="...">
</div>';
$dom = new DomDocument();
@$dom->loadHTML($html_string);
$xpath = new DOMXPath($dom);
echo $xpath->query(
'//div[@class="p-dynamic-image vox-lazy-load lazy-loaded"]/@data-original')
->item(0)->value; // https://cdn2.vox-cdn.com/...
Edit:
Okay try using this (with query instead of xpath)
$xpath = new DOMXpath($page_dom);
$results=$xml->query("//div[@class='p-dynamic-image vox-lazy-load lazy-loaded']/@data-original")->value;
foreach ($results as $image) {
echo $image;
}
Edit again: Note the method you were using before would work for SimpleXML, but for DOM it seems you need to do it within a foreach
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.