java - 如何用正則提取html內容
問題描述
<p class='info-detail-head-classify-subname'><a href='http://m.piao2010.com/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://m.piao2010.com/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>中的內容
相關文章:
1. mac連接阿里云docker集群,已經卡了2天了,求問?2. ddos - apache日志很多其它網址,什么情況?3. 上傳圖片老是失敗是什么原因?SAE_TMP_PATH.后面跟的路徑在哪看4. javascript - 關于jquery的ajax post數據的問題5. 前端 - 我有一個建站程序,但是多個文件夾下的HTML模板代碼沒有進行縮進格式化,請問用什么軟件可以批量格式化一下代碼?6. android-studio - Win10下修改Windows用戶文件夾名user,導致Android Studio報錯無法使用7. phpstudy pro小皮面板經常報這個nginx: [emerg] CreateFile【急】8. thinkphp5.1學習時遇到session問題9. angular.js - angular 路由為什么一直請求css和js文件10. javascript - setTimeout的延遲時間,是從什么時間段開始算起的?
