Menu

How to Extract Hyperlink

Help
samsina
2007-05-15
2013-04-27
  • samsina

    samsina - 2007-05-15

    Imagine that i need to get the link within this part of my html:

    <a href="http://www.cs.ucla.edu">UCLA Computer Science Department</a>

    I need to get the physical link of http://www.cs.ucla.edu but according to what I have (using filters):

                Parser parser = new Parser ("http://www.cs.ucla.edu/~todd/");
               
                NodeList list = new NodeList ();
               
                NodeFilter filter =
                    new AndFilter (
                        new TagNameFilter ("a"),
                        new HasAttributeFilter("href")
                                    );
               
           
                for (NodeIterator e = parser.elements (); e.hasMoreNodes (); ){
               
                    e.nextNode ().collectInto(list, filter);

    this only returns the text between the tags... rather than the link itself. Can someone help me out?

     
    • samsina

      samsina - 2007-05-15

      I found the answer my question.
      Here is what i did....

                Parser parser = new Parser ("http://www.cs.ucla.edu/");
                 
                  NodeList list = new NodeList ();
                 
                 
                  NodeFilter filter =
                      new AndFilter (
                          new TagNameFilter ("a"),
                          new HasAttributeFilter("href")
                                      );
                 
                 
                  list = parser.extractAllNodesThatMatch(filter);
                 
                  for(Integer i = 0; i < list.size(); i ++){
                      String links = ((LinkTag)(list.elementAt (i))).extractLink ();
                      StringBuffer link_buffer = new StringBuffer();
                      link_buffer.append(links);
                      System.out.println (link_buffer);
                  }

       
    • wjsjw

      wjsjw - 2007-11-14

      Hi samsina
        I am a new to HTMLParser. I get the HypertextLinkes with your reply/help.  :)

      best regards

      wjs

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.