JAVA获取html中的所有img链接

 

 

public static  List<String> getImageSrc(String htmlCode) {  
        List<String> imageSrcList = new ArrayList<String>();  
        String regular="<img(.*?)src=\"(.*?)\"";  
        String img_pre="(?i)<img(.*?)src=\"";
        String img_sub="\"";
        Pattern p=Pattern.compile(regular,Pattern.CASE_INSENSITIVE);
        if(StringUtils.isNotBlank(htmlCode)){
            Matcher m = p.matcher(htmlCode);  
            String src = null;  
            while (m.find()) {  
                src=m.group();
                src=src.replaceAll(img_pre, "").replaceAll(img_sub, "").trim();
                imageSrcList.add(src);  
            }      
        }
        return imageSrcList;  
    }

 

posted @ 2021-03-25 14:34  yvioo  阅读(117)  评论(0编辑  收藏  举报