如何使用PHP Simple HTML DOM来获取img src?

时间:2017-03-29 11:28:15

标签: php html parsing dom simple-html-dom

我想从此页面抓取图片http://www.ikea.com/us/en/catalog/products/S59163189/

显示图像的标记如下:

 <div id="moreImgThumbContainer">
    <div class="imageThumb" id="imageThumb_0">
        <a href="javascript:void(0);" id="imageThumbLink_0" class="active">
            <img src="/PIAimages/0386819_PE559167_S3.JPG" onclick="irwStatThumbImgClickedFromPIP();" onmouseover="addOpacityEffect(this.id);" onmouseout="rmvOpacityEffect(this.id);" id="imgID_0" style="opacity: 10;">
        </a> 
    </div>



 <div class="imageThumb" id="imageThumb_1">
        <a href="javascript:void(0);" id="imageThumbLink_1">
            <img src="/PIAimages/0449646_PE599007_S3.JPG" onclick="irwStatThumbImgClickedFromPIP();" onmouseover="addOpacityEffect(this.id);" onmouseout="rmvOpacityEffect(this.id);" id="imgID_1" style="opacity: 0.8;">
        </a>
    </div>
</div>

我尝试使用html代码,在单个页面中,它可以工作。

foreach($html->find('img[id^=imgID]') as $img_link){
         $img_array[] = 'http://www.ikea.com'.$img_link->src;
         $images = implode(';', $img_array); 
}

如何使用此api http://simplehtmldom.sourceforge.net/在PHP html dom解析器中获取每个图像的src

编辑:我认为我发现了问题的原因,图片正在加载ajax并需要时间来加载。

是否有解决方法?

请帮忙!

2 个答案:

答案 0 :(得分:0)

尝试使用jqueryArray#map功能

var d  = $('.imageThumb').map(function (){
c =  $(this).children('a').children('img').attr('src');
return c;
}).get()

console.log(d.join(';'))
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="moreImgThumbContainer">
    <div class="imageThumb" id="imageThumb_0">
        <a href="javascript:void(0);" id="imageThumbLink_0" class="active">
            <img src="/PIAimages/0386819_PE559167_S3.JPG" onclick="irwStatThumbImgClickedFromPIP();" onmouseover="addOpacityEffect(this.id);" onmouseout="rmvOpacityEffect(this.id);" id="imgID_0" style="opacity: 10;">
        </a> 
    </div> 

    <div class="imageThumb" id="imageThumb_1">
        <a href="javascript:void(0);" id="imageThumbLink_1">
            <img src="/PIAimages/0449646_PE599007_S3.JPG" onclick="irwStatThumbImgClickedFromPIP();" onmouseover="addOpacityEffect(this.id);" onmouseout="rmvOpacityEffect(this.id);" id="imgID_1" style="opacity: 0.8;">
        </a>
    </div>
</div>

答案 1 :(得分:0)

检查此代码。我认为它会对你有所帮助

R.id.noSpaceAfter

这是jquery

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="moreImgThumbContainer">
<div class="imageThumb" id="imageThumb_0">
    <a href="javascript:void(0);" id="imageThumbLink_0" class="active">
        <img src="/PIAimages/0386819_PE559167_S3.JPG" onclick="irwStatThumbImgClickedFromPIP(this.id);" onmouseover="addOpacityEffect(this.id);" onmouseout="rmvOpacityEffect(this.id);" id="imgID_0" style="opacity: 10;">
    </a>
</div>

<div class="imageThumb" id="imageThumb_1">
    <a href="javascript:void(0);" id="imageThumbLink_1">
        <img src="/PIAimages/0449646_PE599007_S3.JPG" onclick="irwStatThumbImgClickedFromPIP(this.id);" onmouseover="addOpacityEffect(this.id);" onmouseout="rmvOpacityEffect(this.id);" id="imgID_1" style="opacity: 0.8;">
    </a>
</div>
</div>