PHP DOM遍历在我的情况下失败了

时间:2015-02-15 00:37:19

标签: php dom

我使用http://simplehtmldom.sourceforge.net/抓取date,DOM看起来像这样(或访问此处http://www.vouchercodes.com.my/zalora

我的HTML

<div class="bg-white pdng15 couponInfo">
                   <div class="left1 coupon-box">
           <div align="center" class="height1"><span class="coupon-box-txt1">Save</span> <br>
            <span class="coupon-box-txt">14%</span></div>
           <div class="coupon-box-bg" align="center">Offer</div>
        <div id="div_loading"><img src="/images/ajax-loader.gif" border="0"></div>
<div id="div_Background" style="display:none; background-color:#000000; position:fixed; z-index:101"></div>
<div id="div_Window" style="display:none; top:15%; z-index:105; position:fixed; padding: 8px; width: auto;"></div>
<ul class="commentActions">
        <li id="li_write_comments_15608">
        <a onclick="show_window_comment(15608);" style="" href="javascript:void(0);">0 comment(s)</a></li>
</ul>
           </div>
                   <div class="width60 right"><!--<div class="right bdr4">
                   </div>-->
           <a href="/coupon.php?cId=15608" target="_blank" rel="nofollow"><h3>Women Styles - 14% Off on All Women Lovely Looks</h3></a>
           <p style="text-align: justify;">Use this coupon code and get 14% discount on all women lovely looks accessories from the landing page. Hurry! Click the button below to see products listed under this deal.</p>        <div style="margin-top:5px;">
            <div id="coupon_code_container_15608" style="position: relative; height: 50px; overflow: hidden; cursor: pointer;">

                         <span id="btn_coupon_code_15608" class="getClickBtn" style="border: 0px none rgb(67, 67, 67);">&nbsp;</span>
                         <strong id="coupon_code_15608" class="getCode" code="LVCUPH" style="border: 1px dashed rgb(0, 0, 0);"><span class="code">LVCUPH</span></strong>
                         <script language="javascript">          
                          // With Button
                          $(document).ready(function(){set_copy_command("LVCUPH", "btn_coupon_code_15608", 15608, "zalora", "LVCUPH", "coupon_code_15608");});
                          // Without Button
                          //$(document).ready(function(){set_copy_command("LVCUPH", "coupon_code_15608", 15608, "Zalora", "LVCUPH", "coupon_code_15608");});
                         </script>
                               <div style="position: absolute; left: 0px; top: 0px; width: 190px; height: 39px; z-index: 99;"><embed id="ZeroClipboardMovie_1" src="js/zeroclipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="190" height="39" name="ZeroClipboardMovie_1" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=1&amp;width=190&amp;height=39" wmode="transparent"></div></div>
            <div class="clr-both"></div>
           </div>
           <div class="clr-both"></div>
           <div><strong>Added Date</strong> : 2015-02-12&nbsp;&nbsp;<strong>Expiry Date</strong> : 2015-02-16</div>

           </div>
           <div class="clr-both">&nbsp;</div>
           </div>

我的PHP

$dom = $html->find('.pdng10',0)->find('.couponInfo');

foreach ($dom as $data) {
    $date = $data->find('.width60 div',2)->plaintext;
}

但为什么$ date是&#34;&#34; ?我在上面的代码中看到没有错。如果你想测试,只需删除循环,因为我刚刚发布了部分HTML。

1 个答案:

答案 0 :(得分:0)

试试这个:

    $coupons = $html->find('.couponInfo');
    foreach($coupons as $coupon){
        $div = $coupon->find('.width60', 0);
        // Child 5 contains the Added and Expiry Date
        $child = $div->children[5];

        // Date
        echo $child->plaintext;
    }

为了纠正你的代码,你应该寻找第5个元素而不是第2个元素:

$date = $data->find('.width60 div', 5)->plaintext;