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.
Example:
JsoupGetImages.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 images from HTML using Jsoup. * @author w3schools */ 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("https://www.w3schools.blog/").get(); //Get images from document object. Elements images = document.select("img[src~=(?i)\\.(png|jpe?g|gif)]"); //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")); System.out.println(""); } } catch (IOException e) { e.printStackTrace(); } } } |
Output:
Image Source: https://www.w3schools.blog/wp-content/uploads/2014/08/core-java.jpg Image Height: 91 Image Width: 151 Image Alt Text: core java Image Source: https://www.w3schools.blog/wp-content/uploads/2014/08/servlet.jpg Image Height: 93 Image Width: 157 Image Alt Text: servlet Image Source: https://www.w3schools.blog/wp-content/uploads/2014/08/jsp.jpg Image Height: 94 Image Width: 152 Image Alt Text: jsp Image Source: https://www.w3schools.blog/wp-content/uploads/2014/08/struts.jpg Image Height: 96 Image Width: 155 Image Alt Text: struts Image Source: https://www.w3schools.blog/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.