使用 jsoup java 排除 "No image url"
Exclude the "No image url" using jsoup java
我有以下代码,可以完美地从网页获取图像 URL 然后下载。但是在某个地方找不到图像,它正在存储一个虚拟 png。我希望如果 "No Image Available" 那么它不应该下载图像并跳过它。
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"));
}
示例 HTML 我从中提取图像的代码URL
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">
如果 "No image available" 存在,我该如何修改我的代码以使其跳过?
谢谢
获得 imageElement
后检查属性值并继续下一个元素:
if(imageElement.attr("alt").equals("No image available")){
continue;
}
我有以下代码,可以完美地从网页获取图像 URL 然后下载。但是在某个地方找不到图像,它正在存储一个虚拟 png。我希望如果 "No Image Available" 那么它不应该下载图像并跳过它。
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"));
}
示例 HTML 我从中提取图像的代码URL
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">
如果 "No image available" 存在,我该如何修改我的代码以使其跳过? 谢谢
获得 imageElement
后检查属性值并继续下一个元素:
if(imageElement.attr("alt").equals("No image available")){
continue;
}