Foros del Web » Programación para mayores de 30 ;) » Java »

Extraer links de un documento HTML

Estas en el tema de Extraer links de un documento HTML en el foro de Java en Foros del Web. Buenas Necesito extraer todos los enlaces de un documento html, para ello utilizo la siguiente función: Código: public static LinkedList getLinks(String texto) { LinkedList result ...
  #1 (permalink)  
Antiguo 05/02/2010, 05:04
 
Fecha de Ingreso: febrero-2006
Mensajes: 21
Antigüedad: 18 años, 2 meses
Puntos: 0
Extraer links de un documento HTML

Buenas
Necesito extraer todos los enlaces de un documento html, para ello utilizo la siguiente función:

Código:
public static LinkedList getLinks(String texto) {
           LinkedList result = new LinkedList();
           try
           {
               HTMLEditorKit kit = new HTMLEditorKit();
               HTMLDocument doc = (HTMLDocument)kit.createDefaultDocument();
               doc.putProperty("IgnoreCharsetDirective", Boolean.TRUE);
               StringReader sr = new StringReader(texto);
               kit.read(sr, doc, 0);

               HTMLDocument.Iterator it = doc.getIterator(HTML.Tag.A);
               
               while (it.isValid())
               {                   
                    SimpleAttributeSet s = (SimpleAttributeSet)it.getAttributes();                    
                    String link = (String)s.getAttribute(HTML.Attribute.HREF);
                    if (link != null) {
                        // Agregamos el resultado a la lista
                        if(link.indexOf("localhost")<=0) {
                            result.add(link);
                        }//if
                    }//if
                    it.next();
                }//while
           }//try
           catch (Exception ex)
           {
               System.out.println(ex);
               return null;
           }//catch
           return result;
    }//getLinks
Pero cuando lo hago para <LINK /> en vez de <A></A> me tira esto: java.lang.ClassCastException: javax.swing.text.html.HTMLDocument$RunElement cannot be cast to javax.swing.text.SimpleAttributeSet

Gracias de antemano

Etiquetas: documento, extraer, html, links
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 05:55.