How to Extract Links from a Website using Java

Extract Links from a Website using Java

In this example, We will show you simple program about, How to extract links from a website using Java. The example has been tested and output shared in the same post.

Project Structure

How to Extract Links from a website using Java

Example Program

package com.dineshkrish;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

/**
 * 
 * @author Dinesh Krishnan
 *
 */

public class Example {

	public static void main(String[] args) {

		try {

			URL url = new URL("http://www.dineshkrish.com");

			Document document = Jsoup.parse(url, 5000);

			int size = document.getElementsByTag("a").size();

			for (int i = 0; i < size; i++) {

				Elements elements = document.getElementsByTag("a");

				System.out.println(elements.get(i).attr("href"));
			}

		} catch (MalformedURLException e) {

			e.printStackTrace();
			System.out.println(e.getMessage());
		} catch (IOException e) {

			e.printStackTrace();
			System.out.println(e.getMessage());
		}

	}
}

Maven Dependency (pom.xml)

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.dineshkrish</groupId>
	<artifactId>CommonCollection</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<dependencies>
		
		<!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
		<dependency>
			<groupId>org.jsoup</groupId>
			<artifactId>jsoup</artifactId>
			<version>1.10.2</version>
		</dependency>

	</dependencies>

</project>

Output

ttp://www.dineshkrish.com/log4j-tutorial/hello-world-example-using-log4j/
http://www.dineshkrish.com/javascript/jquery/add-or-remove-class-using-jquery/
http://www.dineshkrish.com/javascript/jquery/change-the-color-of-text-in-html-using-jquery/
http://www.dineshkrish.com/java-rmi-tutorial/call-rmi-service-using-servlet-application/
http://www.dineshkrish.com/javascript/change-opacity-of-html-element-using-javascript/
http://www.dineshkrish.com/web/css3/center-a-div-box-in-the-page-using-css/
http://www.dineshkrish.com/javascript/node-js/create-http-server-in-node-js/
http://www.dineshkrish.com/eclipse/find-class-belongs-to-which-jar-in-eclipse/
http://www.dineshkrish.com/interview-program/java-interview-program-questions/reverse-integer-array-in-java/
http://www.dineshkrish.com/java/join-two-array-using-single-loop-java/
http://www.dineshkrish.com/java/java-collections/convert-enumeration-to-arraylist-in-java/
http://www.dineshkrish.com/java/java-collections/find-minimum-maximum-value-integer-list-java/
http://www.dineshkrish.com/java/java-collections/convert-arraylist-to-enumeration-in-java/
http://www.dineshkrish.com/java/java-collections/read-only-collection-in-java/
http://www.dineshkrish.com/java/java-collections/read-only-map-in-java/
http://www.dineshkrish.com/java/java-collections/read-only-set-in-java/
http://www.dineshkrish.com/java/j2ee/simple-registration-application-using-servlet/
http://www.dineshkrish.com/java/freetts-tutorial/convert-text-to-speech-using-freetts-api/
http://www.dineshkrish.com/java-web-services/soap-tutorial/publish-and-test-simple-soap-web-service-in-java/
http://www.dineshkrish.com/java/java-io-tutorials/how-to-create-directory-in-java/
http://www.dineshkrish.com/java-rmi-tutorial/simple-login-application-in-java-rmi/
http://www.dineshkrish.com/java/j2ee/simple-login-application-using-servlet/
http://www.dineshkrish.com/java/java-collections/convert-primitive-array-to-list-object-in-java/
http://www.dineshkrish.com/java/freetts-tutorial/convert-file-text-to-speech-using-freetts-api/
http://www.dineshkrish.com/frameworks/hibernate/embedded-and-embeddable-example/
http://www.dineshkrish.com/java/utilities/get-image-width-and-height-using-java/
http://www.dineshkrish.com/java/core-java/generating-password-using-java/
http://www.dineshkrish.com/frameworks/spring-tutorial/requestparam-example-in-spring-mvc-framework/
http://www.dineshkrish.com/java/swing/center-a-jframe-window-in-java/
http://www.dineshkrish.com/java/utilities/check-words-spelling-in-java-using-jazzy-api/
http://www.dineshkrish.com/java/utilities/generate-qr-code-using-java/
http://www.dineshkrish.com/itextpdf-tutorial/how-to-convert-text-to-pdf-document-in-java/
http://www.dineshkrish.com/javascript/jquery/dynamically-add-remove-rows-html-table/
http://www.dineshkrish.com/java/java-collections/shuffle-list-in-java/
http://www.dineshkrish.com/java/gson-tutorial/covert-list-to-json-in-java/
http://www.dineshkrish.com/java/core-java/try-with-resource-statement-in-java/
http://www.dineshkrish.com
More….

Hello, folks, I am a founder of dineshkrish.com. I love Java and Open source technologies, If you find my tutorials are useful, please consider making donations to these charities.