# 從一個URL加載一個Document
<div><h2>存在問題</h2>
<p>你需要從一個網站獲取和解析一個HTML文檔,并查找其中的相關數據。你可以使用下面解決方法:</p>
<h2>解決方法</h2>
<p>使用 <code><a title="Creates a new Connection to a URL." href="http://jsoup.org/apidocs/org/jsoup/Jsoup.html#connect%28java.lang.String%29">Jsoup.connect(String
url)</a></code>方法:</p>
<pre><code>Document doc = Jsoup.connect("http://example.com/").get();
String title = doc.title();
</code></pre>
<h2> 說明 </h2>
<p><code><a title="Creates a new Connection to a URL." href="http://jsoup.org/apidocs/org/jsoup/Jsoup.html#connect%28java.lang.String%29">connect(String
url)</a></code> 方法創建一個新的 <code><a title="A Connection provides a convenient interface to fetch content from the web, and parse them into Documents." href="http://jsoup.org/apidocs/org/jsoup/Connection.html">Connection</a></code>,
和 <code><a href="http://jsoup.org/apidocs/org/jsoup/helper/HttpConnection.html#get%28%29">get()</a></code>
取得和解析一個HTML文件。如果從該URL獲取HTML時發生錯誤,便會拋出 IOException,應適當處理。</p>
<p><code><a title="A Connection provides a convenient interface to fetch content from the web, and parse them into Documents." href="http://jsoup.org/apidocs/org/jsoup/Connection.html">Connection</a></code>
接口還提供一個方法鏈來解決特殊請求,具體如下:</p>
<pre><code>Document doc = Jsoup.connect("http://example.com")
.data("query", "Java")
.userAgent("Mozilla")
.cookie("auth", "token")
.timeout(3000)
.post();
</code></pre>
<p>這個方法只支持Web URLs (<code>http</code>和<code>https</code>
協議); 假如你需要從一個文件加載,可以使用 <code><a title="Parse the contents of a file as HTML." href="http://jsoup.org/apidocs/org/jsoup/Jsoup.html#parse%28java.io.File,%20java.lang.String%29">parse(File
in, String charsetName)</a></code> 代替。</p><br></div>
- Introduction
- 爬蟲相關技能介紹
- 爬蟲簡單介紹
- 爬蟲涉及到的知識點
- 爬蟲用途
- 爬蟲流程介紹
- 需求描述
- Http請求處理
- http基礎知識介紹
- http狀態碼
- httpheader
- java原生態處理http
- URL類
- 獲取URL請求狀態
- 模擬Http請求
- apache httpclient
- Httpclient1
- httpclient2
- httpclient3
- httpclient4
- httpclient5
- httpclient6
- okhttp
- OKhttp使用教程
- 技術使用
- java執行javascript
- 網頁解析
- Xpath介紹
- HtmlCleaner
- HtmlCleaner介紹
- HtmlCleaner使用
- HtmlParser
- HtmlParser介紹
- Jsoup
- 解析和遍歷一個HTML文檔
- 解析一個HTML字符串
- 解析一個body片斷
- 從一個URL加載一個Document
- 從一個文件加載一個文檔
- 使用DOM方法來遍歷一個文檔
- 使用選擇器語法來查找元素
- 從元素抽取屬性,文本和HTML
- 處理URLs
- 示例程序 獲取所有鏈接
- 設置屬性的值
- 設置一個元素的HTML內容
- 消除不受信任的HTML (來防止XSS攻擊)
- 正則表達式
- elasticsearch筆記
- 下載安裝elasticsearch
- 檢查es服務健康