java - 如何用正則提取html內容
問題描述
<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>中的內容
相關文章:
1. python - beautifulsoup獲取網頁內容的問題2. Docker for Mac 創建的dnsmasq容器連不上/不工作的問題3. docker鏡像push報錯4. docker - 如何修改運行中容器的配置5. docker-machine添加一個已有的docker主機問題6. fragment - android webView 返回后怎么禁止重新渲染?7. docker不顯示端口映射呢?8. android studio總是在processes running好久9. dockerfile - [docker build image失敗- npm install]10. angular.js - Angular 2 + Django構建的Web應用, 如何合理搭配 ?
