简体   繁体   English

使用 jsoup java 排除“No image url”

[英]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.我有以下代码可以完美地从网页获取图像 URL 然后下载它。 But at someplace the image is not found and it is storing a dummy png.但是在某个地方找不到图像,它正在存储一个虚拟 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我从中提取 imageURL 的示例 HTML 代码

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"> 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= "没有可用的图像" title="没有可用的图像">

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:获得imageElement后,检查属性值并继续下一个元素:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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