Java HtmlParse提取標簽中的值操作
☆代碼示例:
代碼塊語法遵循標準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標簽中的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);
//標簽名過濾器
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 拿取各種標簽資源數(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標簽
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標簽
}
}
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標簽
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標簽
}
}
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標簽
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標簽
}
}
return srcList;
}
}
以上這篇Java HtmlParse提取標簽中的值操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
java僅用30行代碼就實現(xiàn)了視頻轉(zhuǎn)音頻的批量轉(zhuǎn)換
這篇文章主要介紹了java僅用30行代碼就實現(xiàn)了視頻轉(zhuǎn)音頻的批量轉(zhuǎn)換,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
Java 梳理總結(jié)關(guān)于static關(guān)鍵字常見問題
static關(guān)鍵字基本概念我們可以一句話來概括:方便在沒有創(chuàng)建對象的情況下來進行調(diào)用。也就是說:被static關(guān)鍵字修飾的不需要創(chuàng)建對象去調(diào)用,直接根據(jù)類名就可以去訪問,讓我們來了解一下你可能還不知道情況2022-04-04
maven中springboot-maven-plugin的5種打包方式
本文主要介紹了maven中springboot-maven-plugin的5種打包方式,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-09-09
IntelliJ?IDEA2022.3?springboot?熱部署含靜態(tài)文件(最新推薦)
這篇文章主要介紹了IntelliJ?IDEA2022.3?springboot?熱部署含靜態(tài)文件,本文結(jié)合實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-01-01
Spring事務(wù)管理下synchronized鎖失效問題的解決方法
這篇文章主要給大家介紹了關(guān)于Spring事務(wù)管理下synchronized鎖失效問題的解決方法,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用Spring具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2022-03-03
一文搞懂并學(xué)會使用SpringBoot的Actuator運行狀態(tài)監(jiān)控組件的詳細教程
這篇文章主要介紹了一文搞懂并學(xué)會使用SpringBoot的Actuator運行狀態(tài)監(jiān)控組件,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09

