Java 處理 HTML

從網站 read HTML from url by java.net.http.HttpRequest

String url = "http://webcode.me";
HttpRequest request = HttpRequest.newBuilder().uri(URI.create(url)).build();
HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());

System.out.println(response.body());

從網站 read HTML from url by java.net.URL

StringBuilder result = new StringBuilder();
URL url = new URL("https://docs.oracle.com/javase/tutorial/networking/urls/readingURL.html");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String line;
while ((line = in.readLine()) != null) {
    result.append(line);
}
in.close();
System.out.println(result.toString());

以上雖然能取得整份 HTML,但對於處理、撈值還差得遠
因此需要借助 Jsoup 套件

Jsoup

<!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
<dependency>
    <groupId>org.jsoup</groupId>
    <artifactId>jsoup</artifactId>
    <version>1.13.1</version>
</dependency>
Document document;
		
// get document from URL
String url = "https://docs.oracle.com/javase/tutorial/networking/urls/readingURL.html";
document = Jsoup.connect(url).get();

// get document from String
String html = "";
document = Jsoup.parse(html);

// get document from file
String path = "";
document = Jsoup.parse(new File(path), "UTF-8");

// use
Elements elements = document.select("div");
for (Element e : elements) {
    System.out.println(e.text());
}

使用方法相當於大家熟知的 jQuery
可以容易操作 DOM
便於擷取資料