存在問題
你需要從一個網站獲取和解析一個HTML文檔,並查找其中的相關數據。你可以使用下面解決方法:
解決方法
使用 Jsoup.connect(String
url)
方法:
Document doc = Jsoup.connect("http://example.com/").get();
String title = doc.title();
說明
connect(String
url)
方法創建一個新的 Connection
,
和 get()
取得和解析一個HTML文件。如果從該URL獲取HTML時發生錯誤,便會拋出
IOException,應適當處理。
Connection
接口還提供一個方法鏈來解決特殊請求,具體如下:
Document doc = Jsoup.connect("http://example.com")
.data("query", "Java")
.userAgent("Mozilla")
.cookie("auth", "token")
.timeout(3000)
.post();
這個方法只支持Web URLs (http
和https
協議);
假如你需要從一個文件加載,可以使用parse(File
in, String charsetName)
代替。