Hasan shaikh Hasan shaikh - 6 months ago 56
Java Question

How to extract image urls using jsoup

I want to fetch content from a website, such as

title
and
src
attributes of the images. Can any one help me out to achieve this.

Answer

Here is an example of reading all image src and title attributes:

public class Test {

    public static void main(String[] args) {
        try {
            Document doc = Jsoup.connect("http://www.golem.de/").get();
            Elements imageElements = doc.getElementsByTag("img");
            for (Element element : imageElements) {
                System.out.println("src: "+element.attr("src"));
                System.out.println("title: "+element.attr("title"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}