英文:
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("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
答案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("alt").equals("No image available")){
continue;
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论