Java HtmlParse提取標(biāo)簽中的值操作
☆代碼示例:
代碼塊語法遵循標(biāo)準(zhǔn)markdown代碼,例如:
package cas; import org.htmlparser.Node; import org.htmlparser.NodeFilter; import org.htmlparser.Parser; import org.htmlparser.filters.StringFilter; import org.htmlparser.filters.TagNameFilter; import org.htmlparser.tags.ImageTag; import org.htmlparser.util.NodeList; /** * Html 中的body體中提取出Img標(biāo)簽中的src值 * * @author XY * */ public class CASHtmlImgConvert { public static void main(String[] args) { //演示 String[] oldSrcPath=changeImgSrc("<img alt=\"\" src=\"http://www.czb8688.com/attached/image/20160116/20160116141455_775.jpg\" />"); if(oldSrcPath!=null){ for(String str:oldSrcPath){ System.out.println(str); } } } public static boolean isEmpty(String str){ if(str!=null&&(!str.equals(""))) return false; else return true; } /** * * @param htmlPath 本地的html路徑 或者body */ private static String[] changeImgSrc(String htmlPath) { StringBuilder oldSrcPath = new StringBuilder(); try { Parser parser = new Parser(htmlPath); //標(biāo)簽名過濾器 NodeFilter filter = new TagNameFilter ("img"); NodeList nodes = parser.extractAllNodesThatMatch(filter); Node eachNode = null; ImageTag imageTag = null; if (nodes != null) { // 遍歷所有的img節(jié)點(diǎn) for (int i = 0; i < nodes.size(); i++) { eachNode = (Node)nodes.elementAt(i); if (eachNode instanceof ImageTag) { imageTag = (ImageTag)eachNode; // 獲得html文本的原來的src屬性 String path=imageTag.getAttribute("src"); if(path.startsWith("")) path="http://www.czb8688.com"+path; oldSrcPath .append(path+","); } } } } catch (Exception e) { e.printStackTrace(); } String str=oldSrcPath.toString(); //返回圖片數(shù)組 return str.substring(0,str.length()-1).split(","); } }
補(bǔ)充知識:java 掃描HTML 拿取各種標(biāo)簽資源數(shù)據(jù)
直接上代碼,不比比。
package com.zhirui.oa.modules.notice.utils; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; public class TemplateUtil { public static List<Map<String, Object>> getImgSrc(String htmlContent) { List<Map<String, Object>> srcList = new ArrayList<>(); //用來存儲(chǔ)獲取到的地址 Map<String, Object> map = null; Pattern p = Pattern.compile("<(img|IMG)(.*?)(>|></img>|/>)");//匹配字符串中的img標(biāo)簽 Matcher matcher = p.matcher(htmlContent); boolean hasPic = matcher.find(); if (hasPic == true)//判斷是否含有圖片 { while (hasPic) //如果含有圖片,那么持續(xù)進(jìn)行查找,直到匹配不到 { String group = matcher.group(2);//獲取第二個(gè)分組的內(nèi)容,也就是 (.*?)匹配到的 Pattern srcText = Pattern.compile("(src|SRC)=(\"|\')(.*?)(\"|\')");//匹配圖片的地址 Matcher matcher2 = srcText.matcher(group); if (matcher2.find()) { map = new HashMap<>(); map.put("imgResourcePath", matcher2.group(3)); srcList.add(map);//把獲取到的圖片地址添加到列表中 map = null; } hasPic = matcher.find();//判斷是否還有img標(biāo)簽 } } return srcList; } public static List<Map<String, Object>> getVideoSrc(String htmlContent) { List<Map<String, Object>> srcList = new ArrayList<>(); //用來存儲(chǔ)獲取到的視頻地址 Map<String, Object> map = null; Pattern p = Pattern.compile("<(video|VIDEO)(.*?)(>|></video>|/>)");//匹配字符串中的video標(biāo)簽 Matcher matcher = p.matcher(htmlContent); boolean hasPic = matcher.find(); if (hasPic == true)//判斷是否含有視頻 { while (hasPic) //如果含有視頻,那么持續(xù)進(jìn)行查找,直到匹配不到 { String group = matcher.group(2);//獲取第二個(gè)分組的內(nèi)容,也就是 (.*?)匹配到的 Pattern srcText = Pattern.compile("(src|SRC)=(\"|\')(.*?)(\"|\')");//匹配視頻的地址 Matcher matcher2 = srcText.matcher(group); if (matcher2.find()) { map = new HashMap<>(); map.put("videoResourcePath", matcher2.group(3)); srcList.add(map);//把獲取到的視頻地址添加到列表中 map = null; } hasPic = matcher.find();//判斷是否還有video標(biāo)簽 } } return srcList; } public static List<Map<String, Object>> getAhref(String htmlContent) { List<Map<String, Object>> srcList = new ArrayList<>(); //用來存儲(chǔ)獲取到的超鏈接地址 Map<String, Object> map = null; Pattern p = Pattern.compile("<(a|A)(.*?)(>|></a>|/>)");//匹配字符串中的a標(biāo)簽 Matcher matcher = p.matcher(htmlContent); boolean hasPic = matcher.find(); if (hasPic == true)//判斷是否含有超鏈接 { while (hasPic) //如果含有超鏈接,那么持續(xù)進(jìn)行查找,直到匹配不到 { String group = matcher.group(2);//獲取第二個(gè)分組的內(nèi)容,也就是 (.*?)匹配到的 Pattern srcText = Pattern.compile("(href|HREF)=(\"|\')(.*?)(\"|\')");//匹配超鏈接的地址 Matcher matcher2 = srcText.matcher(group); if (matcher2.find()) { map = new HashMap<>(); map.put("aResourcePath", matcher2.group(3)); srcList.add(map);//把獲取到的超鏈接地址添加到列表中 map = null; } hasPic = matcher.find();//判斷是否還有a標(biāo)簽 } } return srcList; } }
以上這篇Java HtmlParse提取標(biāo)簽中的值操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
java僅用30行代碼就實(shí)現(xiàn)了視頻轉(zhuǎn)音頻的批量轉(zhuǎn)換
這篇文章主要介紹了java僅用30行代碼就實(shí)現(xiàn)了視頻轉(zhuǎn)音頻的批量轉(zhuǎn)換,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04Java 梳理總結(jié)關(guān)于static關(guān)鍵字常見問題
static關(guān)鍵字基本概念我們可以一句話來概括:方便在沒有創(chuàng)建對象的情況下來進(jìn)行調(diào)用。也就是說:被static關(guān)鍵字修飾的不需要?jiǎng)?chuàng)建對象去調(diào)用,直接根據(jù)類名就可以去訪問,讓我們來了解一下你可能還不知道情況2022-04-04maven中springboot-maven-plugin的5種打包方式
本文主要介紹了maven中springboot-maven-plugin的5種打包方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-09-09MapStruct實(shí)體間轉(zhuǎn)換的簡單用法
今天小編就為大家分享一篇關(guān)于MapStruct實(shí)體間轉(zhuǎn)換的簡單用法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03IntelliJ?IDEA2022.3?springboot?熱部署含靜態(tài)文件(最新推薦)
這篇文章主要介紹了IntelliJ?IDEA2022.3?springboot?熱部署含靜態(tài)文件,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-01-01Spring事務(wù)管理下synchronized鎖失效問題的解決方法
這篇文章主要給大家介紹了關(guān)于Spring事務(wù)管理下synchronized鎖失效問題的解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Spring具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-03-03一文搞懂并學(xué)會(huì)使用SpringBoot的Actuator運(yùn)行狀態(tài)監(jiān)控組件的詳細(xì)教程
這篇文章主要介紹了一文搞懂并學(xué)會(huì)使用SpringBoot的Actuator運(yùn)行狀態(tài)監(jiān)控組件,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09Spring boot動(dòng)態(tài)修改日志級別的方法
我們經(jīng)常會(huì)遇到業(yè)務(wù)想看debug日志的問題,但是debug日志頻繁打印會(huì)對日志查看有影響,且日志多對系統(tǒng)也會(huì)有一定的壓力,因此,如果可以在需要的時(shí)候動(dòng)態(tài)臨時(shí)調(diào)整下日志的級別則是比較完美的,spring boot已經(jīng)支持這種功能,需要的朋友可以參考下2022-12-12