duanqiang2617 2015-06-06 12:42
浏览 54

从html网站源获取所需数据

I'm reading source code of this link Source

I have below Javascript code

<script type="text/javascript">
jQuery && jQuery(function() {
    setTimeout(function() {
        DL.items_list.initItemAttr(4, '{"sku-SV023435_B_M":"e90dfb84e30edf611e326eeb04d680de6c2de33453d563b7965b97195e855512","sku-SV023435_BL_M":"9594eec95be70e7b1710f730fdda33d96c2de33453d563b7965b97195e855512","sku-SV023435_PU_M":"b9ba865fec061c9706d2fd7ce49c0cc76c2de33453d563b7965b97195e855512"}', '{"price":"5.07","products_dropship_price":"0.0000","has_discount_price":false}', "", '{"sku-SV023435_B_M":7,"sku-SV023435_BL_M":10,"sku-SV023435_PU_M":11}', '{"image_e90dfb84e30edf611e326eeb04d680de":"http:\/\/img1.dresslink.com\/SV\/0\/SV023435-8484-SV023435-G.jpg","image_9594eec95be70e7b1710f730fdda33d9":"http:\/\/img1.dresslink.com\/SV\/0\/SV023435-8484-SV023435-G.jpg","image_b9ba865fec061c9706d2fd7ce49c0cc7":"http:\/\/img1.dresslink.com\/SV\/0\/SV023435-8484-SV023435_PU-G.jpg"}');
    }, 2000);
    var de = $('#w_pro_detail');
    de.find('#switchItem').click(function(){
        var _t = $(this);
        if(_t.is('.narrow')){
            var p_wh = _t.parent().width();
            _t.attr('class','expend').text('>>').parent().animate({left:0,opacity:1},500).css({opacity:1,paddingRight:20});
            _t.parent().find('.p_info,.size_color,.quantity').hide();
            de.css('min-width','0');
            UtilTool.setCookie('isMiniQuickShop', '1', 24);
        }else{
            _t.attr('class','narrow').text('<<').parent().animate({left:10},500);
            _t.parent().find('.p_info,.size_color,.quantity').show();
            de.css('min-width','480px');
            UtilTool.setCookie('isMiniQuickShop', '', 0);
        }

    }).end().mouseover(function(event){
        var elem = $(event.currentTarget),
            fromElem = $(event.target);
        if(elem.has(fromElem).length || fromElem.is(elem)){
            de.stop();
            de.animate({opacity:1},500);
        }

    }).mouseout(function(event){
         var elem = $(event.currentTarget),
            toElem = $(event.target);
        if(!elem.has(toElem).length || toElem.is(elem)){
            de.stop();
            de.animate({opacity:0.15},500);
        }

    });

    var isMiniQuickShop = UtilTool.getCookie('isMiniQuickShop');
    if(isMiniQuickShop == '1') {
        de.css('min-width','0');
        $('#w_pro_detail').find('#switchItem').text('>>').attr('class','expend').end().css({opacity:1,left:0,paddingRight:20}).find('.p_info,.size_color,.quantity').hide();
    }

});
</script>

Out of this code below json part is important for me, it has stock availability for each SKU.

 '{"sku-SV023435_B_M":7,"sku-SV023435_BL_M":10,"sku-SV023435_PU_M":11}'

Now please help me how I can get this part using PHP and preg_match or any suitable function.

Thanks!

Update: The number os SKUs are not same , it differs from one to unlimited. here is another sample :

'{"sku-11430_B_S":"20","sku-11430_B_M":"17","sku-11430_B_L":"30","sku-11430_B_XS":"13","sku-11430_BL_S":"7","sku-11430_BL_M":"17","sku-11430_BL_L":"4","sku-11430_BL_XS":"16","sku-11430_O_S":"8","sku-11430_O_M":"6","sku-11430_O_L":"22","sku-11430_O_XS":"20","sku-11430_LBL_S":"27","sku-11430_LBL_M":"25","sku-11430_LBL_L":"22","sku-11430_LBL_XS":"10","sku-11430_Y_S":"24","sku-11430_Y_M":36,"sku-11430_Y_L":"20","sku-11430_Y_XS":"6","sku-11430_RR_S":"4","sku-11430_RR_M":"35","sku-11430_RR_L":"47","sku-11430_RR_XS":"6"}',
  • 写回答

1条回答 默认 最新

  • dongsou3041 2015-06-07 17:28
    关注

    I suggest extracting these substrings first using preg_match_all, and then parse using json.

    I assume that the JSON objects you are interested in always

    • include all fields starting with sku-
    • values are always integer

    Extracting regex will then look like:

    $re = "#\{(?:,?\"sku-\w+\":\"?\d+\"?)+\}#i";
    

    Code:

    $re = "#\{(?:,?\"sku-\w+\":\"?\d+\"?)+\}#i"; 
    $str = "<<<YOUR_STRING>>>"; 
    preg_match_all($re, $str, $matches);
    foreach ($matches[0] as $val)
    {
       $a = $val;
       $a = json_decode($a);
       print_r($a);
    }
    

    Output:

    stdClass Object
    (
        [sku-11430_B_S] => 20
        [sku-11430_B_M] => 17
        [sku-11430_B_L] => 30
        [sku-11430_B_XS] => 13
        [sku-11430_BL_S] => 7
        [sku-11430_BL_M] => 17
        [sku-11430_BL_L] => 4
        [sku-11430_BL_XS] => 16
        [sku-11430_O_S] => 8
        [sku-11430_O_M] => 6
        [sku-11430_O_L] => 22
        [sku-11430_O_XS] => 20
        [sku-11430_LBL_S] => 27
        [sku-11430_LBL_M] => 25
        [sku-11430_LBL_L] => 22
        [sku-11430_LBL_XS] => 10
        [sku-11430_Y_S] => 24
        [sku-11430_Y_M] => 36
        [sku-11430_Y_L] => 20
        [sku-11430_Y_XS] => 6
        [sku-11430_RR_S] => 4
        [sku-11430_RR_M] => 35
        [sku-11430_RR_L] => 47
        [sku-11430_RR_XS] => 6
    )
    stdClass Object
    (
        [sku-SV023435_B_M] => 7
        [sku-SV023435_BL_M] => 10
        [sku-SV023435_PU_M] => 11
    )
    

    This regex is a bit safer since it may match escaped entities in the sku- part:

    $re = "#\{(?:,?\s*\"sku-[^\"\\]*(?:\\.[^\"\\]*)*\"\s*:\s*\"?\d+\"?\s*)+\}#i";
    

    To extract all JSON objects:

    $re = "#\{\s*\"[^\"\\]*(?:\\.[^\"\\]*)*\"\s*:\s*(?:\"[^"\\]*(?:\\.[^\"\\]*)*\"|\S+)\s*\}#";
    
    评论

报告相同问题?

悬赏问题

  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目