從URL加載文檔

Problem

You need to fetch and parse a HTML document from the web, and find data within it (screen scraping).html

Solution

Use the Jsoup.connect(String url) method:java

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

Description

The connect(String url) method creates a new Connection, and get() fetches and parses a HTML file. If an error occurs whilst fetching the URL, it will throw an IOException, which you should handle appropriately.web

The Connection interface is designed for method chaining to build specific requests:api

Document doc = Jsoup.connect("http://example.com")
  .data("query", "Java")
  .userAgent("Mozilla")
  .cookie("auth", "token")
  .timeout(3000)
  .post();

This method only suports web URLs (http and https protocols); if you need to load from a file, use the parse(File in, String charsetName) method instead.cookie

相關文章
相關標籤/搜索