2013-03-14 23 views
6

Sto cercando di utilizzare l'esempio di crawler di base in crawler4j. Ho preso il codice dal sito web crawler4j here.Perché nell'esempio di crawler4j viene visualizzato un errore?

package edu.crawler; 

import edu.uci.ics.crawler4j.crawler.Page; 
import edu.uci.ics.crawler4j.crawler.WebCrawler; 
import edu.uci.ics.crawler4j.parser.HtmlParseData; 
import edu.uci.ics.crawler4j.url.WebURL; 
import java.util.List; 
import java.util.regex.Pattern; 
import org.apache.http.Header; 

public class MyCrawler extends WebCrawler { 

    private final static Pattern FILTERS = Pattern.compile(".*(\\.(css|js|bmp|gif|jpe?g" + "|png|tiff?|mid|mp2|mp3|mp4" 
        + "|wav|avi|mov|mpeg|ram|m4v|pdf" + "|rm|smil|wmv|swf|wma|zip|rar|gz))$"); 

    /** 
    * You should implement this function to specify whether the given url 
    * should be crawled or not (based on your crawling logic). 
    */ 
    @Override 
    public boolean shouldVisit(WebURL url) { 
      String href = url.getURL().toLowerCase(); 
      return !FILTERS.matcher(href).matches() && href.startsWith("http://www.ics.uci.edu/"); 
    } 

    /** 
    * This function is called when a page is fetched and ready to be processed 
    * by your program. 
    */ 
    @Override 
    public void visit(Page page) { 
      int docid = page.getWebURL().getDocid(); 
      String url = page.getWebURL().getURL(); 
      String domain = page.getWebURL().getDomain(); 
      String path = page.getWebURL().getPath(); 
      String subDomain = page.getWebURL().getSubDomain(); 
      String parentUrl = page.getWebURL().getParentUrl(); 
      String anchor = page.getWebURL().getAnchor(); 

      System.out.println("Docid: " + docid); 
      System.out.println("URL: " + url); 
      System.out.println("Domain: '" + domain + "'"); 
      System.out.println("Sub-domain: '" + subDomain + "'"); 
      System.out.println("Path: '" + path + "'"); 
      System.out.println("Parent page: " + parentUrl); 
      System.out.println("Anchor text: " + anchor); 

      if (page.getParseData() instanceof HtmlParseData) { 
        HtmlParseData htmlParseData = (HtmlParseData) page.getParseData(); 
        String text = htmlParseData.getText(); 
        String html = htmlParseData.getHtml(); 
        List<WebURL> links = htmlParseData.getOutgoingUrls(); 

        System.out.println("Text length: " + text.length()); 
        System.out.println("Html length: " + html.length()); 
        System.out.println("Number of outgoing links: " + links.size()); 
      } 

      Header[] responseHeaders = page.getFetchResponseHeaders(); 
      if (responseHeaders != null) { 
        System.out.println("Response headers:"); 
        for (Header header : responseHeaders) { 
          System.out.println("\t" + header.getName() + ": " + header.getValue()); 
        } 
      } 

      System.out.println("============="); 
    } 
} 

Sopra è il codice per la classe crawler dell'esempio.

public class Controller { 

    public static void main(String[] args) throws Exception { 
      String crawlStorageFolder = "../data/"; 
      int numberOfCrawlers = 7; 

      CrawlConfig config = new CrawlConfig(); 
      config.setCrawlStorageFolder(crawlStorageFolder); 

      /* 
      * Instantiate the controller for this crawl. 
      */ 
      PageFetcher pageFetcher = new PageFetcher(config); 
      RobotstxtConfig robotstxtConfig = new RobotstxtConfig(); 
      RobotstxtServer robotstxtServer = new RobotstxtServer(robotstxtConfig, pageFetcher); 
      CrawlController controller = new CrawlController(config, pageFetcher, robotstxtServer); 

      /* 
      * For each crawl, you need to add some seed urls. These are the first 
      * URLs that are fetched and then the crawler starts following links 
      * which are found in these pages 
      */ 
      controller.addSeed("http://www.ics.uci.edu/~welling/"); 
      controller.addSeed("http://www.ics.uci.edu/~lopes/"); 
      controller.addSeed("http://www.ics.uci.edu/"); 

      /* 
      * Start the crawl. This is a blocking operation, meaning that your code 
      * will reach the line after this only when crawling is finished. 
      */ 
      controller.start(MyCrawler.class, numberOfCrawlers); 
    } 
} 

Sopra è la classe per la classe controller per il web crawler. Quando provo a fare funzionare la classe Controller dal mio IDE (IntelliJ) ottengo il seguente errore:

Exception in thread "main" java.lang.UnsupportedClassVersionError: edu/uci/ics/crawler4j/crawler/CrawlConfig : Unsupported major.minor version 51.0 

C'è qualcosa circa la configurazione Maven che si trova here che dovrei sapere? Devo usare una versione diversa o qualcosa del genere?

+1

Dal suono di ciò, si sta tentando di eseguire una versione del codice che è stata compilata in una versione successiva di Java quindi quella in esecuzione. Per esempio. Il codice è stato compilato con Java 7 e Java 6 in esecuzione o è stato compilato con Java 6 e si sta eseguendo Java 5 ... – MadProgrammer

+0

Controlla http://stackoverflow.com/questions/10382929/unsupported-major-minor-version -51-0 – Farlan

+0

@hey j.jerrod taylor..Sto affrontando il problema nel programma di base. Ricevo un'eccezione Eccezione nel thread "main" java.lang.NoClassDefFoundError: org/apache/http/client/methods/HttpUriRequest \t a com.crawler.web.BasicCrawlController.main (BasicCrawlController.java:78) Causato da: java.lang.ClassNotFoundException: org.apache.http.client.methods.HttpUriRequest, Si prega di suggerire se un altro Jar è anche richiesto. –

risposta

1

Il problema non era con crawler4j. Il problema era che la versione di Java che stavo usando era diversa dall'ultima versione di Java utilizzata in crawler4j. Ho cambiato la versione subito prima di aggiornare a Java 7 e tutto ha funzionato bene. Immagino che l'aggiornamento della mia versione di Java a 7 avrebbe lo stesso effetto.

+0

eseguo la scansione del sito Web dinamico mediante crawler4j (java). http://stackoverflow.com/questions/27264931/crawling-dynamic-website-using-java?noredirect=1#comment43002565_27264931 – BasK

Problemi correlati