Java JsoupXpath 解析HTML

JsoupXpath 可以使用Xpath语法解析HTML,因此我们选择使用它来解析HTML

 

1、添加maven坐标

	  <!--解析html-->
	  <dependency>
		  <groupId>cn.wanghaomiao</groupId>
		  <artifactId>JsoupXpath</artifactId>
		  <version>2.2</version>
	  </dependency>

2、编写解析代码

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import com.google.common.collect.Maps;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.seimicrawler.xpath.JXDocument;
import org.seimicrawler.xpath.JXNode;

import java.io.*;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class HtmlUtil {

	public static String sourceCharset = "GB2312"; // 源文件编码
	public static String targetCharset = "utf8"; // 目标文件编码
	public static String xpath = "//tbody/tr/td//allText()";//Xpath语法
	public static String charsetName = "utf-8";


	public static void readHtml(InputStream in) {
		try {
			String path = getPath(in);
			File file = new File(path);
			encoding(sourceCharset, targetCharset, path);
			Element doc = Jsoup.parse(file, charsetName);
			Elements elements = new Elements();
			elements.add(doc);
			FileUtil.del(path);
			JXDocument jxDocument = new JXDocument(elements);
			List<JXNode> jxNodes = jxDocument.selN(xpath);
            for (JXNode jxNode : jxNodes) {
				System.out.println(jxNode);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 根据InputStream生成文件,返回随机生成路径
	 *
	 * @return
	 */
	public static String getPath(InputStream inputStream) {
		String realPath = System.getProperty("java.io.tmpdir");
		String tmpFolderName = IdUtil.simpleUUID();
		String dir = realPath + "/" + tmpFolderName;
		File file = new File(dir);
		file.mkdir();
		String path = dir + "/" + IdGenerator.nextId() + ".html";
		FileUtil.writeFromStream(inputStream, path);
		return path;
	}
	
	/**
	 * 编码转换
	 *
	 * @param toCharset 要转换的编码
	 * @param path      要转换的文件路径
	 * @return
	 * @throws Exception
	 */
	public static String encoding(String charset, String toCharset, String path) throws Exception {
		File srcFile = new File(path);
		// 编码相同,无需转码
		if (charset.equalsIgnoreCase(toCharset)) {
			return "编码一样,无需转换";
		}
		InputStream in = new FileInputStream(path);
		BufferedReader br = new BufferedReader(
				new InputStreamReader(in, charset));

		StringBuffer sb = new StringBuffer();
		String s1;
		while ((s1 = br.readLine()) != null) {
			String s = URLEncoder.encode(s1, toCharset);
			sb.append(s + "\r\n");//一行+回车
		}
		br.close();
		srcFile.delete();//删除原来文件
		//重新以新编码写入文件并返回值
		File newfile = new File(path);//重新建原来的文件
		newfile.createNewFile();
		OutputStream out = new FileOutputStream(newfile);
		OutputStreamWriter writer = new OutputStreamWriter(out, toCharset);
		BufferedWriter bw = new BufferedWriter(writer);
		bw.write(URLDecoder.decode(sb.toString(), toCharset));
		String result = URLDecoder.decode(sb.toString(), toCharset);
		bw.flush();//刷到文件中
		bw.close();
		return result;
	}
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值