0

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.

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.