Visualizzazione dei risultati da 1 a 4 su 4
  1. #1

    Web Crawler: come estrarre i link da una pagina html e navigare attraverso essi

    Ciao a tutti...sto provando a costruire un web crawler java che visualizzi un grafo di cui ogni nodo rappresenta un link del sito, ma per adesso ho ottenuto scarsi risultati...ho utilizzato un parser per l'HTML che mi estrae tutti i link, ma quando iterativamente li ripasso al metodo estrattore mi dà errore...qualcuno mi può dare una mano

    Ecco il codice del parser HTML che sto utilizzando e che stavo cercando di modificare:
    codice:
    import java.io.*;
    import java.net.*;
    import javax.swing.text.*;
    import javax.swing.text.html.*;
    
    public class GetLinks {
      public static void main(String[] args) {
    
    
        EditorKit kit = new HTMLEditorKit();
        Document doc = kit.createDefaultDocument();
    
        // The Document class does not yet 
        // handle charset's properly.
        doc.putProperty("IgnoreCharsetDirective", 
          Boolean.TRUE);
        try {
    
          // Create a reader on the HTML content.
          Reader rd = getReader("C:/mytool/URLPage.htm");
    
          // Parse the HTML.
          kit.read(rd, doc, 0);
    
          // Iterate through the elements 
          // of the HTML document.
          ElementIterator it = new ElementIterator(doc);
          javax.swing.text.Element elem;
          while ((elem = it.next()) != null) {
            SimpleAttributeSet s = (SimpleAttributeSet)
              elem.getAttributes().getAttribute(HTML.Tag.A);
            if (s != null) {
              System.out.println(
                s.getAttribute(HTML.Attribute.HREF));
    
            }
          }
        } catch (Exception e) {
          e.printStackTrace();
        }
        System.exit(1);
      }
    
    // Returns a reader on the HTML data. If 'uri' begins
    // with "http:", it's treated as a URL; otherwise,
    // it's assumed to be a local filename.
      static Reader getReader(String uri) 
        throws IOException {
        if (uri.startsWith("http:")) {
    
    // Retrieve from Internet.
          URLConnection conn = 
            new URL(uri).openConnection();
          return new 
            InputStreamReader(conn.getInputStream());
        } else {
    
    // Retrieve from file.
          return new FileReader(uri);
        }
      }
    }

  2. #2
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013

    Re: Web Crawler: come estrarre i link da una pagina html e navigare attraverso essi

    Che errore ti dà? io ho appena provato quel codice e "funziona" (cioè mi stampa il contenuto degli href di <a>)
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

  3. #3
    Si infatti il codice funziona, solo ke se provo ad iterare, cioè a passargli cm nuova source il link ke ha appena scoperto mi dà errore....ora t scrivo il codice cn le mie correzioni (le classe Graph e Node fanno parte di una libreria unina2 che dv utilizzare e il main è rikiamato a parte da un'altra classe):
    codice:
    package unina2.parsers.html;
    
    
    import unina2.graph.gui.*;
    
    import java.io.*;
    import java.net.*;
    import javax.swing.text.*;
    import javax.swing.text.html.*;
    
    
    public class translateHTML {
      
    public translateHTML(){}
    
    Graph webGraph ;
    
    public void crawlweb(String source)
    {
    
    webGraph = new Graph();
    
        EditorKit kit = new HTMLEditorKit();
        Document doc = kit.createDefaultDocument();
    
        // The Document class does not yet 
        // handle charset's properly.
        doc.putProperty("IgnoreCharsetDirective", 
          Boolean.TRUE);
        try {
    
    Reader rd = getReader(source);
    
    int startPos = source.indexOf(".") + 1 ;
    int endPos = source.length() ;
    
    String webName = source.substring(startPos,endPos) ;
    
    Node root=webGraph.addNode(webName,150,100,10);
    
          // Parse the HTML.
          kit.read(rd, doc, 0);
    
          // Iterate through the elements 
          // of the HTML document.
          ElementIterator it = new ElementIterator(doc);
          javax.swing.text.Element elem;
          while ((elem = it.next()) != null) {
            SimpleAttributeSet s = (SimpleAttributeSet)elem.getAttributes().getAttribute(HTML.Tag.A);
            if (s != null) {
              System.out.println(s.getAttribute(HTML.Attribute.HREF));
    
    String r=(String)s.getAttribute(HTML.Attribute.HREF);
    
    Node nodofiglio=webGraph.addNode(r,100,50,15);
    webGraph.addEdge(root, nodofiglio,90) ;
    
    crawlweb(r);
    
    
            }
          }
        } catch (Exception e) {
          e.printStackTrace();
        }
        
      }
    
    
    
    // Returns a reader on the HTML data. If 'uri' begins
    // with "http:", it's treated as a URL; otherwise,
    // it's assumed to be a local filename.
      public static Reader getReader(String uri) 
        throws IOException {
        if (uri.startsWith("http:")) {
    
    // Retrieve from Internet.
          URLConnection conn = 
            new URL(uri).openConnection();
          return new 
            InputStreamReader(conn.getInputStream());
        } else {
    
    // Retrieve from file.
    
    int startPos = uri.indexOf("/") + 1 ;
            int endPos = uri.length() ;
    
    String fileName = uri.substring(startPos,endPos) ;
    
          return new FileReader(fileName);
        }
    }
    
    
    
    public Graph getSchemaGraph()
        {
            return webGraph ;
        }
    
    
    }

  4. #4
    Moderatore di Programmazione L'avatar di LeleFT
    Registrato dal
    Jun 2003
    Messaggi
    17,327

    Moderazione

    Usa i tag [*CODE] e [*/CODE] (senza asterisco rosso) per scrivere il codice, così rimane indentato e scritto con un carattere che facilita la lettura.

    Modifico i tuoi post precedenti per sistemare il codice.


    Ciao.
    "Perchè spendere anche solo 5 dollari per un S.O., quando posso averne uno gratis e spendere quei 5 dollari per 5 bottiglie di birra?" [Jon "maddog" Hall]
    Fatti non foste a viver come bruti, ma per seguir virtute e canoscenza

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.