简体   繁体   中英

jsoup extracting data from a table

I have an html file and I need to extract department names from it using 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());
}

I have done smthing like that but it doesnt work.

view-source:http://directory.binghamton.edu/directory/directory.deptlist

Thank you.

Here we go!

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

In your code you select a tag 'deptlist' not the table.
If you want to select all elements with id=deptlist (in my example you select only tables with that id's) you can use this selector: doc.select("#deptlist") .

Look here for some furhter informations: JSoup selector API

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM