nutch getOutLinks 外链的处理

转载自:
http://blog.csdn.net/witsmakemen/article/details/8067530
通过跟踪发现,Fetcher获得网页解析链接没有问题,获得了网页中所有的链接,然后在output()函数中通过FetcherOutputFormat类输出(包含在ParseResult中)。
但是在更新数据库的CrawlDb的update()函数中,发现并没有获得所有的链接,而是部分链接,而且相当一部分链接被过滤掉了。
问题肯定出在FetcherOutputFormat类中,FetcherOutputFormat中负责ParseResult数据输出的是ParseOutputFormat类,
分析ParseOutPutFormat类发现在write()函数中,将ParseResult中的Outlink[]取出,然后将Outlink[]进行处理放到targets链表中,
targets链表最终将输出到crawl_parse中作为CrawlDb的输入,问题就出在将OutLink[]中的数据取出来放到targets的过程中,
因为对每个连接url要进行过滤处理,处理掉格式不正确或者是内部的链接,通过的链接还要进行进一步的规范化过滤处理
规范化由regex
-normalize.xml中的正则表达式过滤,过滤由regex-urlfilter.txt中的正则表达式过滤),
当对url进行规范化的时候过滤掉了很大一部分链接,链接中含有“sid=”被当做session id过滤掉了,所以更新中获得不到这部分链接。 for (int i = 0; i < links.length && validCount < outlinksToStore; i++) { String toUrl = links[i].getToUrl(); // ignore links to self (or anchors within the page) if (fromUrl.equals(toUrl)) {//过滤内部链接 continue; } if (ignoreExternalLinks) {//过滤掉内部链接 try { toHost = new URL(toUrl).getHost().toLowerCase(); } catch (MalformedURLException e) { toHost = null; } if (toHost == null || !toHost.equals(fromHost)) { // external links continue; // skip it } } try { toUrl = normalizers.normalize(toUrl, URLNormalizers.SCOPE_OUTLINK); //规范化url toUrl = filters.filter(toUrl); // 过滤掉url if (toUrl == null) { continue; } } catch (Exception e) { continue; } CrawlDatum target = new CrawlDatum(CrawlDatum.STATUS_LINKED, interval); Text targetUrl = new Text(toUrl); try { scfilters.initialScore(targetUrl, target);//对链接进行打分 } catch (ScoringFilterException e) { LOG.warn("Cannot filter init score for url " + key + ", using default: " + e.getMessage()); target.setScore(0.0f); } targets.add(new SimpleEntry(targetUrl, target)); outlinkList.add(links[i]); validCount++; }
原文地址:https://www.cnblogs.com/i80386/p/3429639.html