Java Jsoup take<;tr>;标记数据

Java Jsoup take<;tr>;标记数据,java,web-crawler,jsoup,Java,Web Crawler,Jsoup,我正在尝试以下代码,预期结果是menuList有一些节点。 但是menuList没有任何节点。为什么呢 public static void main(String[] args) { String connUrl = "http://www.hstree.org/c03/c03_00.php"; try { Document doc = Jsoup.connect(connUrl).get(); Eleme

我正在尝试以下代码,预期结果是menuList有一些节点。 但是menuList没有任何节点。为什么呢

    public static void main(String[] args) {
        String connUrl = "http://www.hstree.org/c03/c03_00.php";
        try {
            Document doc = Jsoup.connect(connUrl).get();
            Elements elements = doc.select("table");
            for (Element element : elements) {
                // System.out.println(element.attributes());
                if (element != null && (element.id().equals("1gn") || element.id().equals("2gn"))) {
                    Node childNode = element.childNodes().get(0);
                    List<Node> menuList = childNode.childNodes();
                    System.out.println(element.id()+" menu");
                    for(Node menu : menuList) {
                        System.out.println(menu.childNodes().get(0).toString());
                        System.out.println(" : " + menu.childNodes().get(1).toString());
                    }
                    System.out.println();
                }
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
publicstaticvoidmain(字符串[]args){
字符串connUrl=”http://www.hstree.org/c03/c03_00.php";
试一试{
Document doc=Jsoup.connect(connUrl.get();
元素=文件选择(“表格”);
for(元素:元素){
//System.out.println(element.attributes());
if(element!=null&(element.id().equals(“1gn”)| | element.id().equals(“2gn”)){
Node childNode=element.childNodes().get(0);
List menuList=childNode.childNodes();
System.out.println(element.id()+“menu”);
用于(节点菜单:菜单列表){
System.out.println(menu.childNodes().get(0.toString());
System.out.println(“:”+menu.childNodes().get(1.toString());
}
System.out.println();
}
}
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}

childNodes
返回包括文本在内的所有内容。您需要使用
children()
Element

String connUrl = "http://www.hstree.org/c03/c03_00.php";
try {
    Document doc = Jsoup.connect(connUrl).get();
    Elements elements = doc.select("table");
    for (Element element : elements) {
        // System.out.println(element.attributes());
        if (element != null && (element.id().equals("1gn") || element.id().equals("2gn"))) {
            Element childNode = element.child(0);
            List<Element> menuList = childNode.children();
            System.out.println(element.id() + " menu");
            for (Element menu : menuList) {
                System.out.println(menu.child(0));
                System.out.println(" : " + menu.child(1));
            }
            System.out.println();
        }
    }
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
String-connUrl=”http://www.hstree.org/c03/c03_00.php";
试一试{
Document doc=Jsoup.connect(connUrl.get();
元素=文件选择(“表格”);
for(元素:元素){
//System.out.println(element.attributes());
if(element!=null&(element.id().equals(“1gn”)| | element.id().equals(“2gn”)){
Element-childNode=Element.child(0);
List menuList=childNode.children();
System.out.println(element.id()+“menu”);
用于(元素菜单:菜单列表){
System.out.println(menu.child(0));
System.out.println(“:”+menu.child(1));
}
System.out.println();
}
}
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}

我认为您的问题应该转到代码审查。请阅读