Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. import java.io.File;
  2. import org.jsoup.Jsoup;
  3. import org.jsoup.parser.Parser;
  4. import org.jsoup.nodes.Element;
  5. import org.jsoup.nodes.Node;
  6. import org.jsoup.nodes.Document;
  7. import org.jsoup.nodes.TextNode;
  8. import org.jsoup.select.Elements;
  9.  
  10.  
  11. public class grabImages {
  12. File input = new File("...HTML");
  13. Document doc = Jsoup.parse(input, "UTF-8", "file:///C:...HTML");
  14.  
  15. Elements img = doc.getElementsByTag("img");
  16. Elements alttext = doc.getElementsByAttribute("alt");
  17.  
  18. for (Element el : img){
  19. if(el.attr("img").contains("alt")){
  20. System.out.println("is the alt text relevant to the image? ");
  21. }
  22.  
  23. else { System.out.println("no alt text found on image");
  24. }
  25. }
  26.  
  27. }
  28.  
  29. el.attr("img")
  30.  
  31. public class Controller {
  32.  
  33. public static void main(String[] args) throws IOException {
  34.  
  35. // Connect to website. This can be replaced with your file loading implementation
  36. Document doc = Jsoup.connect("http://www.google.co.uk").get();
  37.  
  38. // Get all img tags
  39. Elements img = doc.getElementsByTag("img");
  40.  
  41. int counter = 0;
  42.  
  43. // Loop through img tags
  44. for (Element el : img) {
  45. // If alt is empty or null, add one to counter
  46. if(el.attr("alt") == null || el.attr("alt").equals("")) {
  47. counter++;
  48. }
  49. System.out.println("image tag: " + el.attr("src") + " Alt: " + el.attr("alt"));
  50. }
  51. System.out.println("Number of unset alt: " + counter);
  52.  
  53. }
  54.  
  55. }
  56.  
  57. public class grabImages {
  58. public static void main(String[] args) {
  59. Document doc;
  60. try {
  61. doc = Jsoup.connect("...HTML").get();
  62. Elements img = doc.getElementsByTag("img");
  63.  
  64. for (Element el : img){
  65. if(el.hasAttr("alt")){
  66. System.out.println("is the alt text relevant to the image? ");
  67. }
  68. else {
  69. System.out.println("no alt text found on image");
  70. }
  71. }
  72. } catch (IOException e) {
  73. e.printStackTrace();
  74. }
  75. }
  76. }
  77.  
  78. Document doc = Jsoup.connect(url).get();
  79.  
  80. for (Element img : doc.select("img:not([alt])"))
  81. System.out.println("img does not have alt: " + img);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement