如何管理一个爬虫URL的前沿?

How to manage a crawler URL frontier?

伙计们

我有以下代码可以在我的抓取工具上添加已访问的 link。 提取 links 后,我有一个 for 循环 循环遍历每个个体 href 标签 .

在我访问 link 并打开它之后,我会将 URL 添加到上面定义的已访问 link 集合变量中。

private final Collection<String> urlForntier = Collections.synchronizedSet(new HashSet<String>()); 

爬虫实现是多线程的,假设我访问了 100,000 个 url,如果我不终止爬虫,它会一天比一天增长。并且它会产生内存问题?拜托,我有什么选择可以刷新变量而不造成跨线程不一致?

提前致谢!

现代爬虫系统最有用的方法是使用NoSQL数据库。

此解决方案明显比 HashSet 慢。这就是为什么您可以利用不同的缓存策略,例如 Redis, or even Bloom filters

但包括URL的具体性质,我想推荐Trie data structure that gives you lot of options to manipulate and search by url string. (Discussion of java implementation can be found on this Stackoevrflow topic)

  1. 根据问题,我建议使用 Redis to replace use of Collection. It's in-memory database for data structure store and super fast to insert and retrieve data with support of all standard data structures.In your case Set and you can check existence of key in set with SISMEMBER 命令)。

  2. Apache Nutch也很好探索。

如果您的抓取工具再好,快速管理抓取边界就会变得困难、缓慢且容易出错。

幸运的是,您无需自己编写,只需编写您的爬虫程序即可使用 URL Frontier API 并插入适合您的实现。

https://github.com/crawler-commons/url-frontier