首页 > 解决方案 > 用jsoup解析div

问题描述

我有这部分 HTML 代码:

...
<div class="K9wGie">
   <div class="BHMmbe" aria-label="Rated 4.6 stars out of five stars">4.6</div>
   <div class="LvvOW">
    <div aria-label="Rated 4.6 stars out of five stars" role="img">
       <div class="BGNhFb WF1WQd"></div>
       <div class="BGNhFb WF1WQd"></div>
   <span class="EymY4b">
     <span class="O3QoBc hzfjkd"></span>
     <span class="" aria-label="2,749 ratings">2,749</span> total</span></div>
...

我想从第二个 div 中获取值“4.6”,从最后一个跨度中获取值“2,749”。我怎么能用jsoup做到这一点?

试过document.select("div[aria-label]").text()但它返回这个字符串:"Read more Collapse 4.6 Read more Collapse".我什至没有在这个块中看到这些词,但这个 html 代码只是巨大文档的一小部分。

尝试过document.select("div.K9wGie").first().parent().text(),这将返回以下字符串:"4.6 2,758 total 5 4 3 2 1".

试过了

document.select("div.K9wGie").first().parent()
                .select("span").first().text();

它返回2,761 total。如此接近,但我不需要这个“总”字。当然,稍后,如果找不到其他解决方案,我可以使用替换字符串方法,但想用 jsoup 来做。仍然没有解决价值“4.6”的办法。

我接近找到解决方案,但无法击败它。

输出

代码

标签: javaparsingjsoup

解决方案


推荐阅读