Package org.jsoup.examples;
Import Org.jsoup.Jsoup;
Import Org.jsoup.helper.Validate;
Import org.jsoup.nodes.Document;
Import org.jsoup.nodes.Element;
Import org.jsoup.select.Elements;
Import java.io.IOException;
/** * Example program to list links from a URL. */public class Listlinks {public static void main (string[] args) throws IOException {validate.istrue (ARGS.L
Ength = = 1, "Usage:supply URL to fetch");
String URL = args[0];
Print ("Fetching%s ...", url);
Document doc = jsoup.connect (URL). get ();
Elements links = doc.select ("a[href]");
Elements media = doc.select ("[src]");
Elements imports = Doc.select ("link[href]");
Print ("\nmedia: (%d)", media.size ()); for (Element Src:media) {if (Src.tagname (). Equals ("img")) Print ("*%s: <%s>%sx%s" (%
s) ", Src.tagname (), Src.attr (" Abs:src "), src.attr (" width "), src.attr (" height "), Trim (src.attr ("Alt"), 20));
else print ("*%s: <%s>", Src.tagname (), Src.attr ("abs:src"));
Print ("\nimports: (%d)", imports.size ()); for (Element link:imports) {print ("*%s <%s> (%s)", Link.tagname (), Link.attr ("Abs:href"), Link.attr (
"rel"));
Print ("\nlinks: (%d)", links.size ());
for (Element link:links) {print ("* A: <%s> (%s)", Link.attr ("Abs:href"), Trim (Link.text (), 35)); }} private static void print (String msg, Object ... args) {System.out.println (msg,
args)); private static String trim (string s, int width) {if (S.length () > width) return s.substring
(0, Width-1) + ".";
else return s; }
} |