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.comhttp://www.aoyou183.cn/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é)點 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(','); } }
補充知識: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<>(); //用來存儲獲取到的地址 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ù)進行查找,直到匹配不到 {String group = matcher.group(2);//獲取第二個分組的內(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<>(); //用來存儲獲取到的視頻地址 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ù)進行查找,直到匹配不到 {String group = matcher.group(2);//獲取第二個分組的內(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<>(); //用來存儲獲取到的超鏈接地址 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ù)進行查找,直到匹配不到 {String group = matcher.group(2);//獲取第二個分組的內(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)容了,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. ASP常用日期格式化函數(shù) FormatDate()2. html中的form不提交(排除)某些input 原創(chuàng)3. bootstrap select2 動態(tài)從后臺Ajax動態(tài)獲取數(shù)據(jù)的代碼4. 網(wǎng)頁中img圖片使用css實現(xiàn)等比例自動縮放不變形(代碼已測試)5. CSS3中Transition屬性詳解以及示例分享6. python 如何在 Matplotlib 中繪制垂直線7. vue使用moment如何將時間戳轉(zhuǎn)為標(biāo)準(zhǔn)日期時間格式8. js select支持手動輸入功能實現(xiàn)代碼9. jsp文件下載功能實現(xiàn)代碼10. 開發(fā)效率翻倍的Web API使用技巧
