java - 用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”的办法。
我接近找到解决方案,但无法击败它。
解决方案
推荐阅读
- typescript - 打字稿:试图扩展字符串类...无法在我的方法中获取对字符串值的引用
- javascript - useEffect 重新渲染上下文 api 消费者组件
- python - 删除以特定字符串开头的每个单词
- winforms - 在 Windows 桌面应用程序中实现帮助功能的最佳方法 - 限制应用知识库以授权访问
- node.js - AWS lambda 层调用说 opt/ffmpeg 不是目录
- node.js - Nodejs 中的单元测试
- php - PHP类初始化
- multiprocessing - python3.8多进程锁
- sql - 聚合case语句中sum和count的区别
- typescript - 添加类型作为对象的键