JSOUP使用ALT属性查找HTML文件中的所有图像?

SOR*_*SOR 4 html java jsoup

嗨,我相对较新的Java,但我希望编写一个类,使用JSOUP在HTML文件中找到所有ALT(图像)属性.如果图像上没有替代文字,并且有提醒用户检查,我希望打印一条错误消息.

import java.io.File;
import org.jsoup.Jsoup;
import org.jsoup.parser.Parser;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.TextNode;
import org.jsoup.select.Elements;


public class grabImages {
                File input = new File("...HTML");
                Document doc = Jsoup.parse(input, "UTF-8", "file:///C:...HTML");

                Elements img = doc.getElementsByTag("img"); 
                Elements alttext = doc.getElementsByAttribute("alt");

                 for (Element el : img){
                     if(el.attr("img").contains("alt")){
                         System.out.println("is the alt text relevant to the image? ");
                         }

                         else { System.out.println("no alt text found on image");
                         }
                    }

}       
Run Code Online (Sandbox Code Playgroud)

Ben*_*ale 5

我认为你的逻辑有点偏差.

例如:您在这里尝试加载'img'标签的'img'属性...

el.attr("img") 
Run Code Online (Sandbox Code Playgroud)

这是我对该计划的实施.您应该能够根据自己的需要进行更改.

 public class Controller {

        public static void main(String[] args) throws IOException {

            // Connect to website. This can be replaced with your file loading implementation
            Document doc = Jsoup.connect("http://www.google.co.uk").get();

            // Get all img tags
            Elements img = doc.getElementsByTag("img");

            int counter = 0;

            // Loop through img tags
            for (Element el : img) {
                // If alt is empty or null, add one to counter
                if(el.attr("alt") == null || el.attr("alt").equals("")) {
                    counter++;
                }
                System.out.println("image tag: " + el.attr("src") + " Alt: " + el.attr("alt"));
            }
            System.out.println("Number of unset alt: " + counter);

        }

    }
Run Code Online (Sandbox Code Playgroud)