java - 如何用正則提取html內(nèi)容
問題描述
<p class='info-detail-head-classify-subname'><a href='http://www.aoyou183.cn/wenda/11492.html' target='_blank'>財富</a></p> 想用java 提取財富兩個字 請問用正則怎么提取 用jsoup會不會簡單一點
問題解答
回答1:可以使用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 href='http://www.aoyou183.cn/wenda/11492.html' 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)); }} }回答2:
<a[^>]*>([^<]*)</a>
取<a></a>中的內(nèi)容
相關(guān)文章:
1. mysql - 記得以前在哪里看過一個估算時間的網(wǎng)站2. 關(guān)docker hub上有些鏡像的tag被標記““This image has vulnerabilities””3. docker綁定了nginx端口 外部訪問不到4. docker鏡像push報錯5. angular.js - angular內(nèi)容過長展開收起效果6. dockerfile - 為什么docker容器啟動不了?7. 為什么我ping不通我的docker容器呢???8. javascript - 能否讓vue-cli的express修改express重啟服務(wù)9. macos - mac下docker如何設(shè)置代理10. docker-compose中volumes的問題
