如何选择其中没有其他 div 的 div 元素?

发布于 2024-11-29 08:08:31 字数 224 浏览 12 评论 0原文

我正在使用 Java 和 Jsoup 来解析 HTML 页面,我想获取所有不包含其他 div 的 div 来打印它包含的文本。

但例如,如果一个 div 包含一个表,而该表包含一个 div,我就不需要它。我只想要最底层的 div,里面没有其他 div(其他标签也可以)。

我如何做到这一点?

首先,我想知道是否有一些语法可以与 select() 方法一起使用。

I'm using Java and Jsoup to parse HTML pages and I want to get all the divs that not contains another divs inside it to print the text it contains.

But for example, if a div contains a table, and the table costains a div, I don't want it. I want only the div at the most bottom level, with none another div inside it (another tags are ok).

How I do this?

Primarilly, I want to know if there is some syntax that can I use with the select() method.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦太阳 2024-12-06 08:08:31
Document doc; //comes as parameter

Elements divs = doc.getElementsByTag("div");
for(Element div: divs){
    if(div.getElementsByTag("div").size() == 1){
        //is a div with no divs inside it
    }
} 
Document doc; //comes as parameter

Elements divs = doc.getElementsByTag("div");
for(Element div: divs){
    if(div.getElementsByTag("div").size() == 1){
        //is a div with no divs inside it
    }
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文