简体   繁体   中英

Exclude the “No image url” using jsoup java

I have the following code which is working perfectly to get the image URL from a webpage and then download it. But at someplace the image is not found and it is storing a dummy png. I want that if "No Image Available" then it should not download the image and skip it.

        Document document = Jsoup.connect(webpageURL).userAgent("Mozilla/17.0").get();
        Elements elements = document.select("div.img-container.ratio-11-10");

        for (Element e : elements) {
            Element imageElement = e.getElementsByTag("img").first();

            String imageURL = imageElement.attr("abs:src");
            InputStream inputStream = new URL(imageURL).openStream();

            Files.copy(inputStream, Paths.get("src/main/resources/" + ID + ".jpg"));
         }

Sample HTML code from where I am extracting imageURL

img src="https://www.bbcgoodfood.com/sites/default/files/styles/recipe/public/sites/all/themes/bbcw_goodfood/images/dummy-content/member-recipe-icon.png" alt="No image available" title="No image available">

How could I modify my code so that it skips if the "No image available" exist? thanks

After you get the imageElement check the attribute value and continue to the next element:

if(imageElement.attr("alt").equals("No image available")){
    continue;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM