获取html中文档的所有img标签

/**
     * 获取html文档中所有img标签的图片地址
     * @param html
     * @return
     */
    public static List<String> matchImgAddr(String html){
        List<String> addressList = new ArrayList<>();
        Pattern imgPattern = Pattern.compile("<(img|IMG)(.*?)(/>|></img>|>)");
        Matcher imgMatcher = imgPattern.matcher(html);
        boolean isFound = imgMatcher.find();
        if(isFound){
            while (isFound){
                String imgContent = imgMatcher.group(2);
                Pattern srcPattern = Pattern.compile("(src|SRC)=(\"|\')(.*?)(\"|\')");
                Matcher srcMatcher = srcPattern.matcher(imgContent);
                if(srcMatcher.find()){
                    String address = srcMatcher.group(3);
                    addressList.add(address);
                }
                isFound = imgMatcher.find();
            }
        }
        return addressList;
    }
发布了1216 篇原创文章 · 获赞 301 · 访问量 218万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览