Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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");
- }
- }
- }
- 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);
- }
- }
- 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();
- }
- }
- }
- Document doc = Jsoup.connect(url).get();
- for (Element img : doc.select("img:not([alt])"))
- System.out.println("img does not have alt: " + img);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement