溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Java HtmlParse提取標(biāo)簽中的值操作

發(fā)布時(shí)間:2020-10-11 21:50:02 來源:腳本之家 閱讀:102 作者:Sandy林 欄目:開發(fā)技術(shù)

☆代碼示例:

代碼塊語法遵循標(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=\"https://cache.yisu.com/upload/information/20200819/112/18780.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ǔ)充知識(shí):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è)參考,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI