日期:2014-05-17 浏览次数:20648 次
package com.fresheggs.web; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.util.List; import net.htmlparser.jericho.Element; import net.htmlparser.jericho.HTMLElementName; import net.htmlparser.jericho.Source; public class TableContentByHtmlParse { public Element getTableContent(Element element,int rows,int cols) { Element resultElement=null; List<Element> trList=element.findAllElements(HTMLElementName.TR); if(rows<trList.size()) { Element trElement=trList.get(rows); List<Element> tdList=trElement.findAllElements(HTMLElementName.TD); if(cols<tdList.size()) { //Element tdElement=tdList.get(cols); resultElement=tdList.get(cols); } } return resultElement; } public static void test(String url) { Source source=null; try { source=new Source(new URL(url)); } catch(MalformedURLException e) { e.printStackTrace(); } catch(IOException e) { e.printStackTrace(); } List<Element> elementList=source.findAllElements(HTMLElementName.TABLE); for(Element element:elementList) { Element currentElement=new TableContentByHtmlParse().getTableContent(element,2,2); if(currentElement!=null) System.out.println(currentElement.getContent().toString()); } } public static void main(String[] args) { test("http://www.akae.cn/study/resourceShareC.html"); } }