REGEX来自python请求的响应

时间:2014-08-14 15:21:33

标签: python regex

我并不擅长正则表达式,我试图从python-requests响应中获取dict样式字符串。以下是来自html response [cut]的代码片段:

</div>
        </div>


            <div id="image-canvas-caption" class="a-row">
                <div class="a-column a-span12 a-text-center">
                    <span id="canvasCaption" class="a-color-secondary"></span>
                </div>
            </div>           


        <div class="collections-collect-button"></div>

    </div>

</div></div>
</div>

<script type="text/javascript">
maintainHeight = function(){
var mainHolder = document.getElementById("main-image-container");
var imgTagWrapperId = document.getElementById("imgTagWrapperId");
if(mainHolder && typeof mainHolder != 'undefined'){
    var width = mainHolder.offsetWidth;
    var ratio = 0.84;
    var shouldAutoPlay = false;
    var videoSizes = [[262, 445], [294, 500], [323, 550], [356, 606], [399, 679]];

    var containerHeight = width/ratio;
    containerHeight = Math.min(containerHeight, 700);

    mainHolder.style.height = containerHeight + "px";
    if(imgTagWrapperId && typeof imgTagWrapperId !== 'undefined' ){
        imgTagWrapperId.style.height = containerHeight + "px";
    }

    var landingImage = document.getElementById("landingImage");
    var imageHeight = Math.min(containerHeight, 679);
    var imageWidth = Math.min(width, 399);
    if(landingImage){
        landingImage.style.maxHeight = imageHeight + "px";
        landingImage.style.maxWidth  = imageWidth + "px";
    }

    if(shouldAutoPlay){
        if(landingImage){
            var autoPlayWidth=0, autoPlayHeight=0;
            for(var i=0; i < videoSizes.length; i++){
                if( autoPlayWidth < videoSizes[i][0] ){
                    autoPlayWidth = videoSizes[i][0];
                    autoPlayHeight = videoSizes[i][1];
                }
            }
            if( autoPlayWidth > 0 && autoPlayHeight > 0 ){
                var videoRatio = autoPlayWidth/autoPlayHeight;
                // increasing height of the slate image by 1px (and width with the ratio) so that black lines doesn't appear on ipad on right side.
                var isIpad = navigator.userAgent.match(/ipad/i);
                if( isIpad ){
                    containerHeight = containerHeight + 1;
                }
                landingImage.style.width = containerHeight * videoRatio + "px";
                landingImage.style.height = containerHeight + "px";
            }

        }
    }
}
};
maintainHeight();

window.onresize = function(){
maintainHeight();
};
P.when('A').register("ImageBlockATF", function(A){   
var data = {
            'colorImages': { 'initial': [{"hiRes":"http://ecx.images-amazon.com/images/I/717%2BCE-2hWL._UL1500_.jpg","thumb":"http://ecx.images-amazon.com/images/I/41KLRCK3m%2BL._SR38,50_.jpg","large":"http://ecx.images-amazon.com/images/I/41KLRCK3m%2BL.jpg","main":{"http://ecx.images-amazon.com/images/I/717%2BCE-2hWL._UY445_.jpg":[262,445],"http://ecx.images-amazon.com/images/I/717%2BCE-2hWL._UY500_.jpg":[294,500],"http://ecx.images-amazon.com/images/I/717%2BCE-2hWL._UY550_.jpg":[324,550],"http://ecx.images-amazon.com/images/I/717%2BCE-2hWL._UY606_.jpg":[357,606],"http://ecx.images-amazon.com/images/I/717%2BCE-2hWL._UY679_.jpg":[400,679]}},{"hiRes":"http://ecx.images-amazon.com/images/I/61BabEwSG0L._UL1500_.jpg","thumb":"http://ecx.images-amazon.com/images/I/2160xHhT4zL._SR38,50_.jpg","large":"http://ecx.images-amazon.com/images/I/2160xHhT4zL.jpg","main":{"http://ecx.images-amazon.com/images/I/61BabEwSG0L._UX342_.jpg":[342,95],"http://ecx.images-amazon.com/images/I/61BabEwSG0L._UX385_.jpg":[385,107],"http://ecx.images-amazon.com/images/I/61BabEwSG0L._UX425_.jpg":[425,119],"http://ecx.images-amazon.com/images/I/61BabEwSG0L._UX466_.jpg":[466,130],"http://ecx.images-amazon.com/images/I/61BabEwSG0L._UX522_.jpg":[522,146]}}]}, 
            'colorToAsin': {'initial': {}},
            'holderRatio': 0.84,
            'holderMaxHeight': 700,
            'useStretchyImageFix': true
            };
A.trigger('P.AboveTheFold'); // trigger ATF event.    
return data;    

});

我只需要:

'colorImages': { 'initial': [{"hiRes":"http://ecx.images-amazon.com/images/I/717%2BCE-2hWL._UL1500_.jpg","thumb":"http://ecx.images-amazon.com/images/I/41KLRCK3m%2BL._SR38,50_.jpg","large":"http://ecx.images-amazon.com/images/I/41KLRCK3m%2BL.jpg","main":{"http://ecx.images-amazon.com/images/I/717%2BCE-2hWL._UY445_.jpg":[262,445],"http://ecx.images-amazon.com/images/I/717%2BCE-2hWL._UY500_.jpg":[294,500],"http://ecx.images-amazon.com/images/I/717%2BCE-2hWL._UY550_.jpg":[324,550],"http://ecx.images-amazon.com/images/I/717%2BCE-2hWL._UY606_.jpg":[357,606],"http://ecx.images-amazon.com/images/I/717%2BCE-2hWL._UY679_.jpg":[400,679]}},{"hiRes":"http://ecx.images-amazon.com/images/I/61BabEwSG0L._UL1500_.jpg","thumb":"http://ecx.images-amazon.com/images/I/2160xHhT4zL._SR38,50_.jpg","large":"http://ecx.images-amazon.com/images/I/2160xHhT4zL.jpg","main":{"http://ecx.images-amazon.com/images/I/61BabEwSG0L._UX342_.jpg":[342,95],"http://ecx.images-amazon.com/images/I/61BabEwSG0L._UX385_.jpg":[385,107],"http://ecx.images-amazon.com/images/I/61BabEwSG0L._UX425_.jpg":[425,119],"http://ecx.images-amazon.com/images/I/61BabEwSG0L._UX466_.jpg":[466,130],"http://ecx.images-amazon.com/images/I/61BabEwSG0L._UX522_.jpg":[522,146]}}]}

来自var data。我不是很擅长正则表达式,我对此失败了。请帮忙。谢谢。

1 个答案:

答案 0 :(得分:0)

您可以使用此正则表达式:

('colorImages.*)\S

<强> Working demo

enter image description here