【问题标题】:JSOUP find all images in HTML file with ALT attribute?JSOUP在具有ALT属性的HTML文件中查找所有图像?
【发布时间】:2017-09-26 09:40:42
【问题描述】:

您好,我对 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");
                         }
                    }

}       

【问题讨论】:

    标签: java html jsoup


    【解决方案1】:

    我觉得你的逻辑有点不对劲。

    例如: 在这里,您正在尝试加载“img”标签的“img”属性...

    el.attr("img") 
    

    这是我对该程序的实现。您应该能够根据自己的需要对其进行更改。

     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);
    
            }
    
        }
    

    【讨论】:

    • 谢谢这回答了我的问题,我不太确定如何实现 alt,因为它是 img 的一个属性,但这澄清了它。
    • 没问题!如果还有其他困惑,请告诉我。
    【解决方案2】:
    public class grabImages {
          public static void main(String[] args) {
             Document doc;
         try {
             doc = Jsoup.connect("...HTML").get();
             Elements img = doc.getElementsByTag("img"); 
    
              for (Element el : img){
                                    if(el.hasAttr("alt")){
                                        System.out.println("is the alt text relevant to the image? ");
                                    }
                                    else { 
                                        System.out.println("no alt text found on image");
                                    }
                                   }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                   }
    }
    

    el.hasAttr("alt") 将给出 'alt' attr 是否存在。

    更多信息 http://jsoup.org/cookbook/extracting-data/example-list-links

    【讨论】:

      【解决方案3】:

      您可以通过使用CSS selectors 选择没有altimg 来简化此操作,而不是遍历文档中的每个img

          Document doc = Jsoup.connect(url).get();
      
          for (Element img : doc.select("img:not([alt])"))
              System.out.println("img does not have alt: " + img);
      

      【讨论】:

        猜你喜欢
        • 2010-10-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-05
        • 2021-10-19
        • 1970-01-01
        • 2015-08-23
        相关资源
        最近更新 更多