JSOUP使用ALT属性查找HTML文件中的所有图像? [英] JSOUP find all images in HTML file with ALT attribute?

查看:239
本文介绍了JSOUP使用ALT属性查找HTML文件中的所有图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我对Java相对比较陌生,但我希望能够编写一个能够使用JSOUP在HTML文件中查找所有ALT(图像)属性的类。如果图像上没有替代文字,并且提醒用户检查它,我希望打印出错信息。

Hi I am relatively new to Java but I am hoping to write a class that will find all the ALT (image) attributes in a HTML file using JSOUP. I am hoping to get an error message printed if there is no alt text on an image and if there is to remind users to check it.

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

}       


推荐答案

我认为你的逻辑有点偏离。

I think your logic was a little off.

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

For example: Here you are trying to load the 'img' attribute of the 'img' tag...

el.attr("img") 

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

Here's my implementation of the program. You should be able to alter it for your own needs.

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

        }

    }

这篇关于JSOUP使用ALT属性查找HTML文件中的所有图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆