jsoup: Java HTML Parser

1 篇文章 / 0 new
author
jsoup: Java HTML Parser
jsoup API 為方便解析 html 內容的類

► Parse a document from a String

String html = "<html><head><title>First parse</title></head>"
  + "<body>Parsed HTML into a doc.</body></html>";
Document doc = Jsoup.parse(html);

► Parsing a body fragment

String html = "<div>Lorem ipsum.";
Document doc = Jsoup.parseBodyFragment(html);
Element body = doc.body();

► Load a Document from a URL

Document doc = Jsoup.connect("http://example.com/").get();
String title = doc.title();

► Load a Document from a File

File input = new File("/tmp/input.html");
Document doc = Jsoup.parse(input, "UTF-8", "http://example.com/");

► Use DOM methods to navigate a document

File input = new File("/tmp/input.html");
Document doc = Jsoup.parse(input, "UTF-8", "http://example.com/");
 
Element content = doc.getElementById("content");
Elements links = content.getElementsByTag("a");
for (Element link : links) {
  String linkHref = link.attr("href");
  String linkText = link.text();
}

相關說明文件

Free Web Hosting