【问题标题】:Why does the crawler4j example give an error? [closed]为什么 crawler4j 示例会报错? [关闭]
【发布时间】:2013-03-14 00:27:59
【问题描述】:

我正在尝试在 crawler4j 中使用 Basic crawler 示例。我从 crawler4j 网站here 获取代码。

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("=============");
    }
}

以上是示例中爬虫类的代码。

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);
    }
}

上面是网络爬虫的控制器类。 当我尝试从我的 IDE (Intellij) 运行 Controller 类时,我收到以下错误:

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

我应该知道here 中的maven 配置吗?我必须使用不同的版本吗?

【问题讨论】:

  • 从它的声音来看,您正在尝试执行在更高版本的 Java 上编译的代码版本,然后是您正在运行的版本。例如。代码是用 Java 7 编译的,你正在运行 Java 6,或者它是用 Java 6 编译的,你正在运行 Java 5...
  • @hey j.jerrod taylor..我在非常基本的程序中遇到了这个问题。我在线程“main”java.lang.NoClassDefFoundError: org/apache/http/ 中遇到异常异常client/methods/HttpUriRequest at com.crawler.web.BasicCrawlController.main(BasicCrawlController.java:78) Caused by: java.lang.ClassNotFoundException: org.apache.http.client.methods.HttpUriRequest ,请建议是否有其他 Jar也是必需的。
  • @AmritpalSingh 对我来说,问题在于我使用的代码版本是使用与我在计算机上安装的版本不同的 Java 版本编译的。如果您有同样的问题,那么您应该更新您的 Java 版本或使用旧版本的代码。

标签: java crawler4j


【解决方案1】:

问题不在于 crawler4j。问题是我使用的 Java 版本与 crawler4j 中使用的最新版本的 Java 不同。我在他们更新到 Java 7 之前切换了版本,一切正常。我猜我的 Java 版本升级到 7 会有同样的效果。

【讨论】:

猜你喜欢
  • 2017-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多