Skip to content Skip to sidebar Skip to footer

Parsing An Image Url From An Html File

I want to search through a html file and then get the url to an image on that page. This url should then be saved as a string - thats all. The problem is I really don t know how to

Solution 1:

Use JSoup. It's a HTML parser that will allow you to access DOM elements using css selectors (like jQuery).

// Parse your HTML:// 1. From string:Documentdoc= JSoup.parse(htmlAsString);

// 2. Or from an URL:Documentdoc= JSoup.connect("http://my.awesome.site.com/").get();

// Then select images inside it:Elementsimages= doc.select("img");

// Then iteratefor (Element el : images) {
    StringimageUrl= el.attr("src");

    // TODO: Do something with the URL
}

Solution 2:

Take a look at jsoup HTML parser. There is a relevant answer on SO that explains the basic usage of jsoup - https://stackoverflow.com/a/5318771/1321873

Solution 3:

Okay this did the job :) I am getting the image url now:

publicclassjSoupEx {

    privatestaticfinalStringelements=null;

    publicstaticvoidmain(String args[]){


        try {
            Documentdoc= Jsoup.connect("http://***/index.php/Datei:***.jpg").get();
            Elementimage= doc.select("img").first();
            Stringurl= image.absUrl("src");
            System.out.println(url);

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
}

}

Post a Comment for "Parsing An Image Url From An Html File"