JAVA如何获取html中所有img链接呢?
下文笔者讲述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; }
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。