gpt4 book ai didi

java - 如何使用jsoup计算div的数量?

转载 作者:行者123 更新时间:2023-12-01 23:30:19 26 4
gpt4 key购买 nike

如何使用jsoup计算div的数量?

我需要做的是计算所有“news_main”div...

         <h1>Notice to Mariners</h1>
<form name="filter-form" id="filter-form" action="/notice-to-mariners/" enctype="multipart/form-data" accept-charset="UTF-8" method="post">
<div style="display: none"><input type="hidden" name="filter-form" value="1"></div>
<div style="display:none; width:0px; height:0px;"><p><label class="indent" for="filter-form-leave_blank">If you are human leave this blank:</label><input id="filter-form-leave_blank" class="" type="text" name="filter-form-leave_blank" value=""></p></div><div id="filter"><select class="" name="filter" id="filter-form-filter">
<option value="form_error">View notices in force</option>
<option value="1">View notices not in force</option>
<option value="2">View all notices</option>
</select><button type="submit">Filter</button></div><!-- / filter --></form>
<div class="news_main">
<div class="news_main">
<div class="news_main">
<div class="news_main">
<div class="news_main">

等等..等等

我尝试了各种方法,但似乎都返回 0?

代码:

 docNtm = Jsoup.connect("http://www.mhpa.co.uk/notice-to-mariners/").timeout(600000).get();
Elements ntmAmount = docNtm.select("div.news_main div");

System.out.println("size: " + ntmAmount.size());

感谢您的任何建议。

编辑:

我现在可以像这样检索所有 div:

 10-18 22:41:36.365: I/System.out(14624): size:  0
10-18 22:41:36.365: I/System.out(14624): size: 0
10-18 22:41:36.365: I/System.out(14624): size: 0
10-18 22:41:36.365: I/System.out(14624): size: 0
10-18 22:41:36.365: I/System.out(14624): size: 0
10-18 22:41:36.365: I/System.out(14624): size: 0
.....etc

计算它们的最佳方法是什么?

谢谢

最佳答案

使用 Element.getElementsByTag("div");Element.hasClass("news_main");

Document doc = Jsoup.parse(input, "UTF-8", "http://www.mhpa.co.uk/notice-to-mariners/");

Element content = doc.getElementById("content");
Elements divs = content.getElementsByTag("div");
int ntmAmount = 0;
for (Element div : divs) {
if (div.hasClass("news_main"))
ntmAmount++;
}

Element.getElementsByClass("news_main");

...
Elements ntmDivs = content.getElementsByClass("news_main");
int ntmAmount = ntmDivs.size();

关于java - 如何使用jsoup计算div的数量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19438469/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com