java - 如何用正則提取html內(nèi)容
問(wèn)題描述
<p class='info-detail-head-classify-subname'><a href='http://www.cgvv.com.cn/wenda/11492.html' target='_blank'>財(cái)富</a></p> 想用java 提取財(cái)富兩個(gè)字 請(qǐng)問(wèn)用正則怎么提取 用jsoup會(huì)不會(huì)簡(jiǎn)單一點(diǎn)
問(wèn)題解答
回答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.cgvv.com.cn/wenda/11492.html' target='_blank'>財(cái)富</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. docker安裝后出現(xiàn)Cannot connect to the Docker daemon.2. 為什么我ping不通我的docker容器呢???3. android - webview 自定義加載進(jìn)度條4. mysql - 怎么讓 SELECT 1+null 等于 15. javascript - 微信音樂(lè)分享6. 網(wǎng)頁(yè)爬蟲 - 用Python3的requests庫(kù)模擬登陸B(tài)ilibili總是提示驗(yàn)證碼錯(cuò)誤怎么辦?7. javascript - 微信小程序封裝定位問(wèn)題(封裝異步并可能多次請(qǐng)求)8. 并發(fā)模型 - python將進(jìn)程池放在裝飾器里為什么不生效也沒(méi)報(bào)錯(cuò)9. linux - openSUSE 上,如何使用 QQ?10. python 怎樣用pickle保存類的實(shí)例?
