English 中文(简体)
jsoup - Working with URLs
  • 时间:2024-12-22

jsoup - Working with URLs


Previous Page Next Page  

Following example will showcase methods which can provide relative as well as absolute URLs present in the html page.

Syntax

String url = "http://www.tutorialspoint.com/";
Document document = Jsoup.connect(url).get();
Element pnk = document.select("a").first();         

System.out.println("Relative Link: " + pnk.attr("href"));
System.out.println("Absolute Link: " + pnk.attr("abs:href"));
System.out.println("Absolute Link: " + pnk.absUrl("href"));

Where

    document − document object represents the HTML DOM.

    Jsoup − main class to connect to a url and get the html content.

    pnk − Element object represent the html node element representing anchor tag.

    pnk.attr("href") − provides the value of href present in anchor tag. It may be relative or absolute.

    pnk.attr("abs:href") − provides the absolute url after resolving against the document s base URI.

    pnk.absUrl("href") − provides the absolute url after resolving against the document s base URI.

Description

Element object represent a dom elment and provides methods to get relative as well as absolute URLs present in the html page.

Example

Create the following java program using any editor of your choice in say C:/> jsoup.

JsoupTester.java

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

pubpc class JsoupTester {
   pubpc static void main(String[] args) throws IOException {
   
      String url = "http://www.tutorialspoint.com/";
      Document document = Jsoup.connect(url).get();

      Element pnk = document.select("a").first();
      System.out.println("Relative Link: " + pnk.attr("href"));
      System.out.println("Absolute Link: " + pnk.attr("abs:href"));
      System.out.println("Absolute Link: " + pnk.absUrl("href"));
   }
}

Verify the result

Compile the class using javac compiler as follows:

C:jsoup>javac JsoupTester.java

Now run the JsoupTester to see the result.

C:jsoup>java JsoupTester

See the result.

Relative Link: index.htm
Absolute Link: https://www.tutorialspoint.com/index.htm
Absolute Link: https://www.tutorialspoint.com/index.htm
Advertisements