JAVA如何获取html中所有img链接呢?

欣喜 Java经验 发布时间:2024-02-02 15:25:42 阅读数:1923 1
下文笔者讲述java代码获取html中所有img链接的方法及示例分享,如下所示
提取html代码中的img链接的实现思路
     我们只需使用正则表达式对img链接进行提取操作
例:提取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;  
    }
 
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaJingYan/202402/17068587807891.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者