Heim > Artikel > Backend-Entwicklung > javascript - 关于Jsoup解析html巨大问题!难倒我了,谢谢各位。
按题主给出的html,可以这样:
<code>Document document = Jsoup.parse(html); Elements elements = document.select("td br:lt(4)"); for (Element element : elements) { System.out.println(element.previousSibling()); } </code>
应题主需求,可以改为:
<code> Document document = Jsoup.parse(html); Elements elements = document.select("td"); for (Element element : elements) { Elements brs = element.select("br:lt(4)"); if(StringUtils.isNotBlank(brs.toString())) for (Element br : brs) { System.out.print(br.previousSibling().toString().concat(" ")); } else System.out.println(element.html()); } </code>