加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – Jsoup:在无CSS的HTML中提取两个块之间的所有HTML

发布时间:2020-12-15 03:11:33 所属栏目:Java 来源:网络整理
导读:使用Jsoup在符合此模式的两个块之间提取所有 HTML(字符串,文档或元素)的最佳方法是什么: strong {any HTML could appear here,except for a strong pair}/strong ... {This is the HTML I need to extract. any HTML could appear here,except for a strong
使用Jsoup在符合此模式的两个块之间提取所有 HTML(字符串,文档或元素)的最佳方法是什么:
<strong>
 {any HTML could appear here,except for a <strong> pair}
</strong>

 ...
 {This is the HTML I need to extract. 
  any HTML could appear here,except for a <strong> pair}
 ... 

<strong>
 {any HTML could appear here,except for a <strong> pair}
</strong>

使用正则表达式这可能很简单,如果我将它应用于整个body.html():

(<strong>.+</strong>)(.+)(<strong>.+</strong>)
                       ^
                       +----- There I have my HTML content

但是,正如我从similar challenge中学到的那样,如果我使用已经解析过Jsoup的DOM,性能可以提高(即使代码稍微长一点) – 除了这次没有Element.nextSibling()和Element.nextElementSibling()都可以来救援.

例如,我在Jsoup中搜索了类似jQuery的nextUntil,但是找不到类似的东西.

是否有可能提出比上述基于正则表达式的方法更好的东西?

解决方法

我不知道它是否更快,但也许这样的东西会起作用:
Elements strongs = doc.select("strong");
Element f = strongs.first();
Element l = strongs.last();
Elements siblings = f.siblingElements();
List<Element> result = siblings.subList(siblings.firstIndexOf(f) + 1,siblings.lastIndexOf(l));

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读