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/

How to Add or Remove Class using JQuery

How to Change the Color of Text in HTML using JQuery

How to Call RMI Service using Servlet Application

How to Change Opacity of HTML Element using Javascript

How to Center a Div Box in the Page using CSS

How to Create HTTP Server in Node JS

How to find class belongs to which JAR in Eclipse

How to Reverse Integer Array in Java Program

How to Join Two Array using Single Loop in Java

How to Convert Enumeration to ArrayList in Java

How to find Minimum and Maximum value from Integer List in Java

How to Convert ArrayList to Enumeration in Java

How to make Read Only Collection in Java Example

How to make Read Only Map in Java Example

How to make Read Only Set in Java Example

Simple Registration Application using Servlet, JSP, and JDBC Example

How to Convert Text to Speech using FreeTTS API

How to Publish and Test Simple SOAP Web Service in Java

How to create directory in Java

Simple Login Application in Java RMI

Simple Login Application using Servlet, JSP, and JDBC Example

How to Convert Primitive Array to List Object in Java

How to Convert File Text to Speech using FreeTTS API

@Embedded and @Embeddable Example in Hibernate

How to Get Image Width and Height using Java

Generating Password using Java Program Example

@RequestParam Example in Spring MVC Framework

How to Center a JFrame Window in Java

How to Check Words Spelling in Java using Jazzy API

How to Generate QR Code using Java

How to Convert Text to PDF Document in Java

Dynamically Add or Remove Rows in HTML Table using JQuery

How to Shuffle List in Java Language

How to Convert List to JSON in Java using Gson API

Try with Resource Statement using AutoCloseable Example in Java


http://www.dineshkrish.com
More….