[英]Jsoup take <tr> tag data
I am trying out the following code and i expected the result that menuList has some Nodes.我正在尝试下面的代码,我预计 menuList 有一些节点的结果。 But menuList doesn't have any Node.
但是 menuList 没有任何节点。 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();
}
}
childNodes
returns everything including text. childNodes
返回包括文本在内的所有内容。 You need to use children()
and Element
:您需要使用
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.