Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从JSOUP中的元素中删除元素_Java_Arraylist_Web Scraping_Jsoup_Elements - Fatal编程技术网

Java 如何从JSOUP中的元素中删除元素

Java 如何从JSOUP中的元素中删除元素,java,arraylist,web-scraping,jsoup,elements,Java,Arraylist,Web Scraping,Jsoup,Elements,我试图解析网页中给定标题下的所有内容。(不应包括以下标题下的数据) 我尝试了以下方法: ->(以某种方式)选择所需的标题(带子标题) ->删掉下一个标题(我们只需要当前标题) 第二步代码: //prune out next headings (we only need current heading for( int tempIndex = 1; tempIndex < theseMayContainLinks.size(); tempIndex++ ){//start f

我试图解析网页中给定标题下的所有内容。(不应包括以下标题下的数据)

我尝试了以下方法:

->(以某种方式)选择所需的标题(带子标题)

->删掉下一个标题(我们只需要当前标题)

第二步代码:

//prune out next headings (we only need current heading
        for( int tempIndex = 1; tempIndex < theseMayContainLinks.size(); tempIndex++ ){//start from next element
            if(theseMayContainLinks.get(tempIndex).toString().contains(currentElement.tagName())){
                for(int removeIndex = tempIndex; removeIndex < theseMayContainLinks.size(); removeIndex++){
                    theseMayContainLinks.remove(removeIndex);
                }
            }
        }
仔细观察类。你需要的是一个合适的组合符。例如,如果你想选择标题后面的div,你可以使用“h3+div”,如果你想选择两者,可以使用“h3,h3+div”

然后,您希望使用正则表达式定义所需的确切标题(请参阅同一页上的伪选择器)

    String heading = "1-Dimensional Kinematics";
    String cssQuery = "h3:containsOwn(" + heading + "), h3:containsOwn(" + heading + ") + div";
    Elements elements = doc.select(cssQuery);

您想获取特定标题和相应的div吗?是的,实际上我想获取与给定标题对应的所有div。非常感谢您的帮助。有些标题有两个div。对于时间,我刚才在查询中使用了
+div+div