Cheap VPS & Xen Server

Residential Proxy Network - Hourly & Monthly Packages

Get Links of URL


In this example, we will print the total links of an URL. To do so, we are going to call select() method of Document class that returns the reference of Elements. The Elements class have elements that can be traversed by for-each loop. The Element class provides attr() and text() methods to return link and text of the link.

  1. import java.io.IOException;
  2. import org.jsoup.Jsoup;
  3. import org.jsoup.nodes.Document;
  4. import org.jsoup.nodes.Element;
  5. import org.jsoup.select.Elements;
  6. public class JsoupPrintLinks {
  7.      public static void main( String[] args ) throws IOException{
  8.             Document doc = Jsoup.connect(“http://www.Kreationnext.com”).get();
  9.             Elements links = doc.select(“a[href]”);
  10.             for (Element link : links) {
  11.                 System.out.println(“\nlink : ” + link.attr(“href”));
  12.                 System.out.println(“text : ” + link.text());
  13.             }
  14. }
  15. }

Output:

Comments

comments