我是 Java 网络编程的新手,我正在尝试制作一个网络爬虫,使用 Crawler4j示例代码
我的问题是当我提交repost请求时,Crawling任务(这是一个阻塞任务)需要一些时间才能完成,Heroku托管有3秒的请求超时,这使得无法运行同步抓取任务,相同的程序在我的本地机器上运行良好。
根据我的阅读,不可能通过基本/免费提供来更改 Heroku 的超时。
我想知道是否可以将其作为异步任务启动(我知道可以使用 CrawlerController.startNonBlocking() 函数)并等待它完成,以便我可以显示爬行操作的结果。
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
String url = request.getParameter("url");
CrawlConfig config = new CrawlConfig();
String crawlStorageFolder = "/tmp/temp_storage";
config.setCrawlStorageFolder(crawlStorageFolder);
int numberOfCrawlers = 1;
config.setPolitenessDelay(1);
config.setMaxDepthOfCrawling(2);
config.setMaxPagesToFetch(5);
config.setResumableCrawling(false);
PageFetcher pageFetcher = new PageFetcher(config);
RobotstxtConfig robotstxtConfig = new RobotstxtConfig();
RobotstxtServer robotstxtServer = new RobotstxtServer(robotstxtConfig, pageFetcher);
CrawlController controller = null;
try {
controller = new CrawlController(config, pageFetcher, robotstxtServer);
} catch(Exception e){
e.printStackTrace();
}
controller.addSeed(url);
controller.start(Crawler.class, numberOfCrawlers);
// Methods showing the results of the crawling ...
}