Easy learning with example program codes

Jsoup get images from HTML example

Let us discuss how to get images 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 images from document object.
4. Iterate the images.
5. Print images attributes.


import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
 * This class is used get images from HTML using Jsoup.
 * @author codesjava
public class JsoupGetImages {
  public static void main(String args[]){
    Document document;
    try {
    	//Get Document object after parsing the html from given url.
	document = Jsoup.connect("").get();
	//Get images from document object.
	Elements images = 
	//Iterate images and print image attributes.
	for (Element image : images) {
	  System.out.println("Image Source: " + image.attr("src"));
	  System.out.println("Image Height: " + image.attr("height"));
	  System.out.println("Image Width: " + image.attr("width"));
	  System.out.println("Image Alt Text: " + image.attr("alt"));
    } catch (IOException e) {


Image Source:
Image Height: 91
Image Width: 151
Image Alt Text: core java
Image Source:
Image Height: 93
Image Width: 157
Image Alt Text: servlet
Image Source:
Image Height: 94
Image Width: 152
Image Alt Text: jsp
Image Source:
Image Height: 96
Image Width: 155
Image Alt Text: struts
Image Source:
Image Height: 94
Image Width: 152
Image Alt Text: hibernate

Download this example.
Next Topic: Jsoup get metadata from HTML example.
Previous Topic: Jsoup get links from HTML example.

Related Topics:

How to build java project using ant in eclipse?
JAXB marshalling – convert java object to xml example.
How to create pdf file in java using iText jar?
Generics class example.
OGNL in struts 2.
Hibernate One-to-One Mapping using xml.
Send inline image in email using JavaMail API.
Quartz 2 JobListener example.


Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at


We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on

Copyright © 2019 CodesJava Protection Status SiteMap Reference: Java Wiki