[英]JSoup parsing a text file containing a html table with Java
我真的不确定如何获取需要放入数据库的信息,下面的代码仅打印整个文件。
File input = new File("shipMove.txt");
Document doc = Jsoup.parse(input, null);
System.out.println(doc.toString());
我的HTML是这里从61行,我需要得到的列标题下的项目,但也抢MMSI编号不是列标题下,但在href标记。 除了从网页获取HTML外,我没有使用过JSoup。 我只能真正看到使用php的教程,而宁愿不使用它。
为了获得这些信息,最好的方法是使用Jsoup的选择器API 。 使用选择器,您的代码将类似于以下内容(伪代码!):
File input = new File("shipMove.txt");
Document doc = Jsoup.parse(input, null);
Elements matches = doc.select("<your selector here>");
for( Element element : matches )
{
// do something with found elements
}
这里有一个很好的文档: 使用选择器语法查找元素 。 如果仍然卡住,请描述您的问题。
以下是该选择器的一些提示 ,您可以使用:
// Select the table with class 'shipinfo'
Elements tables = doc.select("table.shipinfo");
// Iterate over all tables found (since it's only one, you can use first() instead
for( Element element : tables )
{
// Select all 'td' tags of that table
Elements tdTags = element.select("td");
// Iterate over all 'td' tags found
for( Element td : tdTags )
{
// Print it's text if not empty
final String text = td.text();
if( text.isEmpty() == false )
{
System.out.println(td.text());
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.