| Usuario |
Parsear Pagina |
Desconocido
|
| Enviado - 2/10/2004 |
| |
| Algun Ejemplo que me permita ver como parsear datos de una pagina web, para poder incorporarlos a un objeto en JAVA.....Gracias |
| |
|
RIBAN
8 Mensaje(s)
|
| Enviado - 25/10/2004 |
| |
Te mando un ejemplo sacando links:
import java.io.*;
import java.util.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
import javax.swing.text.html.HTMLEditorKit.*;
import javax.swing.text.html.parser.*;
import java.net.*;
public class SacaEnlaces extends ParserCallback {
String dir = "";
String cadenalarga = "http://www.google.com";
public static void main(String[] args){
new SacaEnlaces();
}
public SacaEnlaces(){
super();
this.dir=dir;
try{
HTMLEditorKit.Parser parser = new ParserDelegator();
URL urlObject = new URL(cadenalarga);
URLConnection con = urlObject.openConnection();
con.setRequestProperty( "User-Agent", "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0; H010818)" );
System.out.println( "length: "+con.getContentLength() );
System.out.println( "content: "+con.getContent() );
System.out.println( "type: "+con.getContentType() );
InputStream st = con.getInputStream();
Reader reader = new InputStreamReader(st);
parser.parse(reader, this, true);
}catch(Exception e){
System.out.println("ERROR=== " + e.getMessage());
}
}
public void handleStartTag(HTML.Tag t, MutableAttributeSet a, int pos) {
if (HTML.Tag.A == t){
Enumeration e = a.getAttributeNames();
while(e.hasMoreElements()) {
try{
HTML.Attribute nombre = (HTML.Attribute) e.nextElement();
String valor = (String) a.getAttribute(nombre);
if(nombre == HTML.Attribute.HREF) {
if(valor.length()>6){
if(valor.startsWith("http://")){ System.out.println(valor); }
}
}
}catch(Exception f){}
}
}
}
}
|
| |
|
|
|