crawler4j使用类CrawlConfig编译错误 - 期望VariableDeclaratorId

时间:2013-08-08 06:48:22

标签: crawler4j

代码无法编译。我把JRE改为1.7。编译器没有突出显示Eclipse中的类,并且CrawlConfig似乎在编译器中失败。该类应该从Linux的命令行运行。

有什么想法吗?

编译器错误 - 描述资源路径位置类型 令牌“crawlStorageFolder”上的语法错误,此令牌之后的VariableDeclaratorId期望zeocrawler.java / zeowebcrawler / src / main / java / com / example第95行Java问题

import edu.uci.ics.crawler4j.crawler.CrawlConfig;
import edu.uci.ics.crawler4j.crawler.CrawlController;
import edu.uci.ics.crawler4j.crawler.Page;
import edu.uci.ics.crawler4j.crawler.WebCrawler;
import edu.uci.ics.crawler4j.fetcher.PageFetcher;
import edu.uci.ics.crawler4j.parser.HtmlParseData;
import edu.uci.ics.crawler4j.robotstxt.RobotstxtConfig;
import edu.uci.ics.crawler4j.robotstxt.RobotstxtServer;
import edu.uci.ics.crawler4j.url.WebURL;

    public class Controller {   

         String crawlStorageFolder = "/data/crawl/root";
        int numberOfCrawlers = 7;

        CrawlConfig config = new CrawlConfig();

        config.setCrawlStorageFolder(crawlStorageFolder);

        PageFetcher pageFetcher = new PageFetcher(config);
        RobotstxtConfig robotstxtConfig = new RobotstxtConfig();
        RobotstxtServer robotstxtServer = new RobotstxtServer(robotstxtConfig, pageFetcher);
        CrawlController controller = new CrawlController(config, pageFetcher, robotstxtServer);

           controller.addSeed("http://www.senym.com");
            controller.addSeed("http://www.merrows.co.uk");
            controller.addSeed("http://www.zeoic.com");


            controller.start(MyCrawler.class, numberOfCrawlers);








    }
    public URLConnection connectURL(String strURL) {
        URLConnection conn =null;
        try {
        URL inputURL = new URL(strURL);
        conn = inputURL.openConnection();
            int test = 0;

        }catch(MalformedURLException e) {
        System.out.println("Please input a valid URL");
        }catch(IOException ioe) {
        System.out.println("Can not connect to the URL");
        }
        return conn;
        }

    public static void updatelongurl()
    {

//      System.out.println("Short URL: "+ shortURL);
//        urlConn =  connectURL(shortURL);
 //       urlConn.getHeaderFields();
 //       System.out.println("Original URL: "+ urlConn.getURL());

/* connectURL - This function will take a valid url and return a 
URL object representing the url address. */



    }






    public class MyCrawler extends WebCrawler {

        private      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) {          
                String url = page.getWebURL().getURL();
                System.out.println("URL: " + url);

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

2 个答案:

答案 0 :(得分:0)

这是一个非常奇怪的错误,因为代码看起来很干净。尝试在命令行上使用-clean选项启动eclipse。

答案 1 :(得分:0)

更改

String crawlStorageFolder = "/data/crawl/root";

String crawlStorageFolder = "./data/crawl/root";

即。添加前导.