我正在使用来自http://code.google.com/p/crawler4j/的一些爬虫代码。
现在,我要做的是从另一个类访问在 MyCrawler 类中找到的每个 URL。
我启动爬虫:
// * 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);
当我尝试使用“return”来获取我的 URL 时,我收到此错误:
The return type is incompatible with WebCrawler.visit(Page)
它要求我将类型更改为“无效”,但我当然不想这样做。
这是我遇到问题的功能:
@Override
public String visit(Page page) {
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());
return url;
}
我也尝试使用吸气剂,但由于它是“阻塞操作”,它不起作用。我的想法不多了。