SukruK SukruK - 1 month ago 14
Java Question

how to get image using html parsing with jsoup

I want get all images using html parsing with jsoup.
I use below code ;

Elements images = doc.select("img[src~=(?i)\\.(jpe?g)]");
for (Element image : images) {

//System.out.println("\nsrc : " + image.attr("src"));
arrImageItem.add(image.attr("src"));

}


I parse this method all images but i want to parse this url


http://tvrehberi.hurriyet.com.tr/images/742/403742.jpg


I want to parse beginnig of this url


http://tvrehberi.hurriyet.com.tr/images .... .jpg


How to get parse like this ?

Answer

This will probably give you what you ask for, though your question is a bit unclear, so I can't be sure.

public static void main(String args[]){

    Document doc = null;
    String url = "http://tvrehberi.hurriyet.com.tr";
    try {
        doc = Jsoup.connect(url).get();
    } catch (IOException e1) {
        e1.printStackTrace();
    }

    for (Element e : doc.select("img[src~=(?i)\\.(jpe?g)]")) {
        if(e.attr("src").startsWith("http://tvrehberi.hurriyet.com.tr/images")){
            System.out.println(e.attr("src"));
        }
    }
}

So, this might not be a very "clean" solution, but the if-statement will make sure it only prints out the image URL's from the /images/-directory on the server.