Advertisement
Kvarz

Count external links on one web-page

Aug 25th, 2014
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.26 KB | None | 0 0
  1. import java.io.*;
  2. import java.net.URL;
  3. import java.util.Enumeration;
  4.  
  5. import javax.swing.text.MutableAttributeSet;
  6. import javax.swing.text.html.*;
  7. import javax.swing.text.html.parser.*;
  8.  
  9. public class Test {
  10.    public static void main(String[] args) throws Exception  {
  11.       Reader r = null;
  12.  
  13.       try   {
  14.          URL u = new URL(args[0]);
  15.          InputStream in = u.openStream();
  16.          r = new InputStreamReader(in);
  17.  
  18.          ParserDelegator hp = new ParserDelegator();
  19.          hp.parse(r, new HTMLEditorKit.ParserCallback() {
  20.             public void handleStartTag(HTML.Tag t, MutableAttributeSet a, int pos) {
  21.                // System.out.println(t);
  22.                if(t == HTML.Tag.A)  {
  23.                   Enumeration attrNames = a.getAttributeNames();
  24.                   StringBuilder b = new StringBuilder();
  25.                   while(attrNames.hasMoreElements())    {
  26.                       Object key = attrNames.nextElement();
  27.                       if("href".equals(key.toString())) {
  28.                           System.out.println(a.getAttribute(key));
  29.                       }
  30.                   }
  31.                }
  32.             }
  33.          }, true);
  34.       }finally {
  35.          if(r != null)  {
  36.             r.close();
  37.          }
  38.       }
  39.    }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement