<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 在 Java 中讀取網頁 原文:http://zetcode.com/articles/javareadwebpage/ 在 Java 中讀取網頁是一個教程,它提供了幾種讀取 Java 中網頁的方法。 它包含六個示例,這些示例從一個很小的網頁上下載 HTTP 源。 ## Java 讀取網頁工具 Java 具有內置的工具和第三方庫,用于讀取/下載網頁。 在示例中,我們使用 URL,JSoup,`HtmlCleaner`,Apache `HttpClient`,Jetty `HttpClient`和 HtmlUnit。 在以下示例中,我們從 [something.com](http://something.com) 微型網頁下載 HTML 源。 ## 讀取帶有 URL 的網頁 `URL`表示一個統一資源定位符,它是指向萬維網上資源的指針。 `ReadWebPageEx.java` ```java package com.zetcode; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; public class ReadWebPageEx { public static void main(String[] args) throws MalformedURLException, IOException { BufferedReader br = null; try { URL url = new URL("http://www.something.com"); br = new BufferedReader(new InputStreamReader(url.openStream())); String line; StringBuilder sb = new StringBuilder(); while ((line = br.readLine()) != null) { sb.append(line); sb.append(System.lineSeparator()); } System.out.println(sb); } finally { if (br != null) { br.close(); } } } } ``` 該代碼示例讀取網頁的內容。 ```java br = new BufferedReader(new InputStreamReader(url.openStream())); ``` `openStream()`方法打開到指定 URL 的連接,并返回`InputStream`以從該連接讀取。 `InputStreamReader`是從字節流到字符流的橋梁。 它讀取字節,并使用指定的字符集將其解碼為字符。 另外,`BufferedReader`用于獲得更好的性能。 ```java StringBuilder sb = new StringBuilder(); while ((line = br.readLine()) != null) { sb.append(line); sb.append(System.lineSeparator()); } ``` 使用`readLine()`方法逐行讀取 HTML 數據。 源被附加到`StringBuilder`上。 ```java System.out.println(sb); ``` 最后,`StringBuilder`的內容被打印到終端。 ## 使用 JSoup 讀取網頁 JSoup 是流行的 Java HTML 解析器。 ```java <dependency> <groupId>org.jsoup</groupId> <artifactId>jsoup</artifactId> <version>1.9.2</version> </dependency> ``` 我們已經使用了這種 Maven 依賴關系。 `ReadWebPageEx2.java` ```java package com.zetcode; import java.io.IOException; import org.jsoup.Jsoup; public class ReadWebPageEx2 { public static void main(String[] args) throws IOException { String webPage = "http://www.something.com"; String html = Jsoup.connect(webPage).get().html(); System.out.println(html); } } ``` 該代碼示例使用 JSoup 下載并打印一個很小的網頁。 ```java String html = Jsoup.connect(webPage).get().html(); ``` `connect()`方法連接到指定的網頁。 `get()`方法發出 GET 請求。 最后,`html()`方法檢索 HTML 源。 ## 使用`HtmlCleaner`讀取網頁 `HtmlCleaner`是用 Java 編寫的開源 HTML 解析器。 ```java <dependency> <groupId>net.sourceforge.htmlcleaner</groupId> <artifactId>htmlcleaner</artifactId> <version>2.16</version> </dependency> ``` 對于此示例,我們使用`htmlcleaner` Maven 依賴項。 `ReadWebPageEx3.java` ```java package com.zetcode; import java.io.IOException; import java.net.URL; import org.htmlcleaner.CleanerProperties; import org.htmlcleaner.HtmlCleaner; import org.htmlcleaner.SimpleHtmlSerializer; import org.htmlcleaner.TagNode; public class ReadWebPageEx3 { public static void main(String[] args) throws IOException { URL url = new URL("http://www.something.com"); CleanerProperties props = new CleanerProperties(); props.setOmitXmlDeclaration(true); HtmlCleaner cleaner = new HtmlCleaner(props); TagNode node = cleaner.clean(url); SimpleHtmlSerializer htmlSerializer = new SimpleHtmlSerializer(props); htmlSerializer.writeToStream(node, System.out); } } ``` 該示例使用`HtmlCleaner`下載網頁。 ```java CleanerProperties props = new CleanerProperties(); props.setOmitXmlDeclaration(true); ``` 在屬性中,我們設置為省略 XML 聲明。 ```java SimpleHtmlSerializer htmlSerializer = new SimpleHtmlSerializer(props); htmlSerializer.writeToStream(node, System.out); ``` `SimpleHtmlSerializer`將創建結果 HTML,而不會進行任何縮進和/或壓縮。 ## 使用 Apache `HttpClient`讀取網頁 Apache `HttpClient`是 HTTP/1.1 兼容的 HTTP 代理實現。 它可以使用請求和響應過程來抓取網頁。 HTTP 客戶端實現 HTTP 和 HTTPS 協議的客戶端。 ```java <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.2</version> </dependency> ``` 我們將這種 Maven 依賴關系用于 Apache HTTP 客戶端。 `ReadWebPageEx4.java` ```java package com.zetcode; import java.io.IOException; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.util.EntityUtils; public class ReadWebPageEx4 { public static void main(String[] args) throws IOException { HttpGet request = null; try { String url = "http://www.something.com"; HttpClient client = HttpClientBuilder.create().build(); request = new HttpGet(url); request.addHeader("User-Agent", "Apache HTTPClient"); HttpResponse response = client.execute(request); HttpEntity entity = response.getEntity(); String content = EntityUtils.toString(entity); System.out.println(content); } finally { if (request != null) { request.releaseConnection(); } } } } ``` 在代碼示例中,我們將 GET HTTP 請求發送到指定的網頁并接收 HTTP 響應。 從響應中,我們讀取了 HTML 源代碼。 ```java HttpClient client = HttpClientBuilder.create().build(); ``` 生成了`HttpClient`。 ```java request = new HttpGet(url); ``` `HttpGet`是 HTTP GET 方法的類。 ```java request.addHeader("User-Agent", "Apache HTTPClient"); HttpResponse response = client.execute(request); ``` 執行 GET 方法并接收到`HttpResponse`。 ```java HttpEntity entity = response.getEntity(); String content = EntityUtils.toString(entity); System.out.println(content); ``` 從響應中,我們獲得網頁的內容。 ## 使用 Jetty `HttpClient`讀取網頁 Jetty 項目也有一個 HTTP 客戶端。 ```java <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-client</artifactId> <version>9.4.0.M1</version> </dependency> ``` 這是 Jetty HTTP 客戶端的 Maven 依賴項。 `ReadWebPageEx5.java` ```java package com.zetcode; import org.eclipse.jetty.client.HttpClient; import org.eclipse.jetty.client.api.ContentResponse; public class ReadWebPageEx5 { public static void main(String[] args) throws Exception { HttpClient client = null; try { client = new HttpClient(); client.start(); String url = "http://www.something.com"; ContentResponse res = client.GET(url); System.out.println(res.getContentAsString()); } finally { if (client != null) { client.stop(); } } } } ``` 在示例中,我們使用 Jetty HTTP 客戶端獲取網頁的 HTML 源。 ```java client = new HttpClient(); client.start(); ``` 創建并啟動`HttpClient`。 ```java ContentResponse res = client.GET(url); ``` GET 請求發布到指定的 URL。 ```java System.out.println(res.getContentAsString()); ``` 使用`getContentAsString()`方法從響應中檢索內容。 ## 使用 HtmlUnit 讀取網頁 HtmlUnit 是用于測試基于 Web 的應用的 Java 單元測試框架。 ```java <dependency> <groupId>net.sourceforge.htmlunit</groupId> <artifactId>htmlunit</artifactId> <version>2.23</version> </dependency> ``` 我們使用這種 Maven 依賴關系。 `ReadWebPageEx6.java` ```java package com.zetcode; import com.gargoylesoftware.htmlunit.WebClient; import com.gargoylesoftware.htmlunit.WebResponse; import com.gargoylesoftware.htmlunit.html.HtmlPage; import java.io.IOException; public class ReadWebPageEx6 { public static void main(String[] args) throws IOException { try (WebClient webClient = new WebClient()) { String url = "http://www.something.com"; HtmlPage page = webClient.getPage(url); WebResponse response = page.getWebResponse(); String content = response.getContentAsString(); System.out.println(content); } } } ``` 該示例下載一個網頁并使用 HtmlUnit 庫進行打印。 在本文中,我們使用各種工具(包括 URL,JSoup,`HtmlCleaner`,Apache `HttpClient`,Jetty `HttpClient`和 HtmlUnit)在 Java 中抓取了一個網頁。 您可能也對以下相關教程感興趣: [Java 教程](/lang/java/), [Java Servlet 讀取網頁](/articles/javaservletreadwebpage/),[如何使用 Java 讀取文本文件](/articles/javagetpostrequest/) ,[發送 HTTP GET POST 請求 ]使用 Java](/articles/javareadtext/),[使用 Java8 的`StringJoiner`來連接字符串](/articles/java8stringjoiner/),[Jetty 教程](/java/jetty/)或 [Google Guava 簡介](/articles/guava/)。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看