简体   繁体   English

jsoup从表中提取数据

[英]jsoup extracting data from a table

I have an html file and I need to extract department names from it using jsoup. 我有一个html文件,我需要使用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 查看源代码: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. 在代码中,选择一个标签“ deptlist”而不是表格。
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") . 如果要选择id=deptlist所有元素(在我的示例中,仅选择具有该id的表),则可以使用以下选择器: doc.select("#deptlist")

Look here for some furhter informations: JSoup selector API 在这里查找更多信息: JSoup选择器API

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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