排除“没有图像网址”使用jsoup java

huangapple go评论76阅读模式
英文:

Exclude the "No image url" using jsoup java

问题

以下是您要的翻译内容:

        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 altText = imageElement.attr("alt");

            if (!altText.equalsIgnoreCase("No image available")) {
                String imageURL = imageElement.attr("abs:src");
                InputStream inputStream = new URL(imageURL).openStream();

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

从提取图像URL的示例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">

您可以通过在循环内添加条件来检查图像元素的"alt"属性,以跳过"No image available"的情况。如果"alt"属性的值不是"No image available",则下载图像。

英文:

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(&quot;Mozilla/17.0&quot;).get();
        Elements elements = document.select(&quot;div.img-container.ratio-11-10&quot;);

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

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

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

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

答案1

得分: 1

在获取到 imageElement 后,检查其属性值并继续处理下一个元素:

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

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

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

</details>



huangapple
  • 本文由 发表于 2020年5月29日 05:29:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/62074853.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定