Jsoup get links from HTML

Let us discuss how to get links from HTML using Jsoup API with the help of below example.

Follow the below steps:

1. Use connect(String url) method of Jsoup class which returns the connection of specified URL.
2. Use get() method of Connection class which returns Document object.
3. Get links from document object.
4. Iterate the links.
5. Print link attributes.

Example:

JsoupGetLinks.java

import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
 
/**
 * This class is used get links from HTML using Jsoup.
 * @author w3schools
 */
public class JsoupGetLinks {
  public static void main(String args[]){
    Document document;
    try {
    	//Get Document object after parsing the html from given url.
	document = Jsoup.connect("https://www.w3schools.blog/").get();
 
	//Get links from document object. 
	Elements links = document.select("a[href]");
 
	//Iterate links and print link attributes.
	for (Element link : links) {
		System.out.println("Link: " + link.attr("href"));
		System.out.println("Text: " + link.text());
		System.out.println("");
	}
 
    } catch (IOException e) {
	e.printStackTrace();
    }		
  }
}

Output:

Link: https://www.w3schools.blog
Text: w3schools
 
Link: https://www.w3schools.blog/
Text: Home
 
Link: https://www.w3schools.blog/core-java-tutorial/
Text: Core Java
 
Link: https://www.w3schools.blog/servlet-tutorial/
Text: Servlet
 
Link: https://www.w3schools.blog/jsp-tutorial/
Text: JSP
 
Link: https://www.w3schools.blog/struts-2-tutorial/
Text: Struts2
 
Link: https://www.w3schools.blog/hibernate-tutorial/
Text: Hibernate
 
Link: https://www.w3schools.blog/java-mail-api-tutorial/
Text: Java Mail
 
Link: https://www.w3schools.blog/quartz-scheduler-tutorial/
Text: Quartz Scheduler
...

Download this example.
 
Next Topic: Jsoup get images from HTML example.
Previous Topic: Jsoup get title from HTML example.