java - 如何用正则提取html内容
本文介绍了java - 如何用正则提取html内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
<p class="info-detail-head-classify-subname"><a id="info_detail_head_classify_type" href="" target="_blank">财富</a></p>
想用java 提取财富两个字 请问用正则怎么提取
用jsoup会不会简单一点
解决方案
可以使用jsoup和regex, 推荐使用jsoup!
jsoup document:
https://jsoup.org/cookbook/in...
http://www.open-open.com/jsoup/
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
// 方法1: jsoup
String html = "<p class=\"info-detail-head-classify-subname\"><a id=\"info_detail_head_classify_type\" href=\"\" target=\"_blank\">财富</a></p>";
Document doc = Jsoup.parse(html);
Element element = doc.getElementById("info_detail_head_classify_type");
System.out.println(element.text());
// 方法2: regex
Pattern r = Pattern.compile("<a.*>(.*)</a>");
Matcher m = r.matcher(html);
if (m.find()) {
System.out.println(m.group(1));
}
}
}
这篇关于java - 如何用正则提取html内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文