Java正则表达式抽取网页script标签之间的内容

tsang1990 tsang1990 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

String regex = "<[\\s]*?script type=\"text/tp[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>";
String htmlURL = "http://mshupeidian.bjx.com.cn/mnews/20170711/836347.shtml";
// String html = "<script type=\"text/tp1\" id=\"tp1\"><p><strong>95、发电源设点功率按什么原则计算?</strong></p><p>答:电源设点功率是根据ACE的大小按不同原则计算。ACE按其大小分为死区、正常分配区、允许控制区及紧急支援区。对不同的区域有不同的分配策略。</p></script>";
String html = HtmlUtils.getUserHtmlContent(htmlURL);//返回的网页源码
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(html);
while(matcher.find()){
System.out.println(matcher.group(0));
}

如题,想利用正则表达式提取网页<script type="text/tp1" id="tp1"></script>间的内容,采用如上regex表达式时发现返回的结果:
<script type="text/tp1" id="tp1">
<p><strong>95、发电源设点功率按什么原则计算?</script>
<script type="text/tp2" id="tp2">
<p><strong>107、什么是能量管理系统(EMS)?其主要功能是什么?</script>
<script type="text/tp3" id="tp3">
<p><strong>122、电力系统电压调整的常用方法有几种?</script>
<script type="text/tp4" id="tp4">
<p><strong>137、电力变压器停、送电操作,应注意哪些事项?</script>

问题即遇到源码含半边 /> 就停止匹配了,不能抽取完整结果,但同样的方法将上述的匹配内容换为注释的内容时有能准确抽取,尝试直接在网页源码进行转义双引号(即
String html = HtmlUtils.getUserHtmlContent(htmlURL).replaceAll("\"","\"");
)但依然无法抽取
!请问如何才能抽取完整结果,万谢!!
...全文
评论 1
收藏 已收藏
分享
返回顶部
tsang1990
Java 51,407 成员 86,131 内容