• March 3, 2015
  • Jsoup
  • Comments Off on Jsoup get images from HTML example

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 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 images from HTML using Jsoup.
 * @author javawithease
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("http://tutorialspointexamples.com/").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: http://tutorialspointexamples.com/wp-content/uploads/2014/08/core-java.jpg
Image Height: 91
Image Width: 151
Image Alt Text: core java
Image Source: http://tutorialspointexamples.com/wp-content/uploads/2014/08/servlet.jpg
Image Height: 93
Image Width: 157
Image Alt Text: servlet
Image Source: http://tutorialspointexamples.com/wp-content/uploads/2014/08/jsp.jpg
Image Height: 94
Image Width: 152
Image Alt Text: jsp
Image Source: http://tutorialspointexamples.com/wp-content/uploads/2014/08/struts.jpg
Image Height: 96
Image Width: 155
Image Alt Text: struts
Image Source: http://tutorialspointexamples.com/wp-content/uploads/2014/08/hibernate.jpg
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.


Comments are closed.