我曾经想过尝试写一个简单的爬虫,它可以爬行,并为我们的NPO的网站和内容生成一个搜索结果列表。
有没有人对如何做到这一点有什么想法?你从哪里开始抓取爬虫?它是如何传回它的发现并继续爬行的呢?它如何知道它找到了什么,等等。
发布于 2008-09-19 15:25:22
可以肯定的是,你将重新发明轮子。但这里有一些基础知识:
将它们放在永久存储中,这样您就可以停止和启动crawler,而不会丢失状态。
算法是:
while(list of unvisited URLs is not empty) {
take URL from list
remove it from the unvisited list and add it to the visited list
fetch content
record whatever it is you want to about the content
if content is HTML {
parse out URLs from links
foreach URL {
if it matches your rules
and it's not already in either the visited or unvisited list
add it to the unvisited list
}
}
}发布于 2011-12-19 21:45:22
爬虫的复杂部分是如果你想要将它扩展到大量的网站/请求。在这种情况下,您将不得不处理一些问题,例如:
)(适用于任何类型的爬虫)。
和一些重要的事情
在每次请求时,
发布于 2012-12-12 23:40:05
多线程网络爬虫
如果你想抓取大型网站,那么你应该编写一个多线程的爬虫。在文件/数据库中连接、获取和写入抓取的信息-这是抓取的三个步骤,但如果你使用单线程,那么你的CPU和网络利用率将会很高。
多线程网络爬虫需要两个数据结构-linksVisited(这应该实现为hashmap或trai)和linksToBeVisited(这是一个队列)。
网络爬虫使用BFS遍历万维网。
一个基本的网络爬虫算法:
下面是关于如何同步线程的代码片段……
public void add(String site) { synchronized (this) { if (!linksVisited.contains(site)) { linksToBeVisited.add(site);} public String next() { if (linksToBeVisited.size() == 0) { return null;}已同步(此){ //如果(linksToBeVisited.size() > 0) { String s= linksToBeVisited.get(0);linksToBeVisited.remove(0);linksVisited.add(s);return s;} return null;} },则需要再次检查大小是否已更改
https://stackoverflow.com/questions/102631
复制相似问题