如何通过正则表达式在脚本中获取json中的信息?

时间:2017-08-31 07:03:57

标签: python regex

我有一个JSON,我想在其中获得一些信息。 使用beautifulsoup和regex。

<script charset="gbk">
    var pageConfig = {
        compatible: true,
        product: {
            modules: [
                'address',
                'prom',
                'colorsize',
                'buytype',
                'baitiao',
                'summary',
                'o2o',
                'buybtn',
                'track',
                'suits',
                'crumb',
                'fittings',
                'detail',
                'contact',
                'popbox',
                'preview',
                'info',
                'imcenter',
                'jdservice',
                'commitments',
                'gift',
                'popupCar'
            ],
                    skuid: 11157027932,                                  
                    cat: [670,671,672],
                    commentVersion:'888',        
                    twoColumn: false,                
                    isFeeType: true,        
                    isCloseLoop:true,                                
                    isBookMvd4Baby: false,       
                    addComments:true,
                    mainSkuId:'10122299306',        
                    foot: '//dx.3.cn/footer?type=common_config2',
                    shangjiazizhi: false        }
    };
                            try {
                    function is_sort_black_list() {
          var jump_sort_list = {"6881":3,"1195":3,"10011":3,"6980":3,"12360":3};
          if(jump_sort_list['670'] == 1 || jump_sort_list['671']==2 || jump_sort_list['672']==3) {
            return false;
          }
          return false;
        }


</script>

我希望在这个JSON中得到“cat”和“VenderID”,我怎样才能通过正则表达式得到这些? 我需要信息[670,671,672]和skuid:11157027932

2 个答案:

答案 0 :(得分:0)

cat: ([\[\d,\]]+),

会在第一个捕获组中获得skuid的值,请参阅regex demo

private isDuplicateNik(nik: number): Promise<boolean>{
    return this.employeeService.isDuplicateNik(nik).then(res => res < 0 ? false : true );
}

将获得第一个捕获组中cat的值,请参阅regex demo

答案 1 :(得分:0)

我用BS4获取这个脚本的汤,

        for ss in thissoup.find_all('script')[0:1]:
        print ss

ss是剧本。

        m = re.search(r"(?s)var\s+pageConfig\s*=\s*(\{.*?\});", ss)
        print m.group(1)

比我想要使用它,但错误发生了。 我想得到一个obj,我可以得到像cat = Obj [“cat”]

这样的信息
相关问题