在此介紹一個還不錯的Java HTML Parser元件 - jsoup,引用它,可以快速地將HTML內容Parsing出你要的結果,以下簡單範例介紹。
import java.net.URL;
import java.util.Iterator;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
public class Parser {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
// URL
URL url = new URL("http://tw.yahoo.com");
// Create the Document Object
Document doc = Jsoup.parse(url, 3000);
// Get first table
Element table = doc.select("table").first();
// Get td Iterator
Iterator<Element> ite = table.select("td").iterator();
// Print content
int cnt = 0;
while(ite.hasNext())
{
cnt++;
System.out.println("Value " + cnt + ": " + ite.next().text());
}
}
}
在此範例將yahoo的第一個Table中所有td的值Print出來,還有很多應用可自行參考。
jsoup官方網站
文章標籤
全站熱搜
