Search code examples
javaweb-crawlerjsoup

Jsoup take <tr> tag data


I am trying out the following code and i expected the result that menuList has some Nodes. But menuList doesn't have any Node. Why is that?

    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();
        }

    }

Solution

  • childNodes returns everything including text. You need to use children() and 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();
    }