2012-12-12 111 views
0

我有一个html文件,我需要使用jsoup从它中提取部门名称。jsoup从表中提取数据

Document doc = Jsoup.connect("http://directory.binghamton.edu/directory/directory.deptlist").get(); 
System.out.println(doc); 
Elements departments = doc.select("deptlist"); 

for (Element department : departments) { 
    System.out.println(department.text()); 
} 

我已经做了这样的事情,但它不起作用。

视图出处:http://directory.binghamton.edu/directory/directory.deptlist

谢谢。

回答

1

这里我们开始吧!

Document doc = Jsoup.connect("http://directory.binghamton.edu/directory/directory.deptlist").get(); 

Elements departments = doc.select("table#deptlist a"); // Select all 'a' in a 'table'-tag with id 'deptlist' 
String name; 


for(Element element : departments) // Iterate over all Elements available 
{ 
    name = element.text(); // Save the plaintext (no html) of the element 
    System.out.println(name); // Simple output (as an example) 
} 

在你的代码中选择一个标签 'deptlist' 不表。
如果你想选择所有元素id=deptlist(在我的例子中,你只选择那个ID的表),你可以使用这个选择器:doc.select("#deptlist")

在这里寻找一些furhter信息:JSoup selector API