Nutch是Apache组织的一个开源项目,利用它用户可以建立自己内部网的搜索引擎,也可以建立针对整个网络的搜索引擎。
两年前Nutch0.9版本的时候我写了个针对Linux的安装介绍,最近nutch1.2出来了,很想试试有什么新功能,可惜没有专门的Linux服务器,就在Windows上安装吧。(我使用的操作系统是windows 2003 server)
安装与配置过程比一般软件复杂,比如需要了解简单Linux命令、Tomcat服务器配置等,但当你看到自己亲手建立的搜索引擎时,一切付出都是值得的!
1.下载并安装JDK 网址http://www.oracle.com/technetwork/java/javase/downloads/index.html 我用的是JDK6_23 ,别忘了在环境变量中设置JAVA_HOME,否则后面运行Nutch时会报错
2.下载并安装Tomcat 网址http://tomcat.apache.org/ 我装的是Tomcat7.0.6。我安装的是exe版本,省得配置环境变量了。安装完成后输入http://127.0.0.1:8080 出现变态猫就ok了,可以进入下一步。
3.到http://www.cygwin.com/ 点setup.exe下载并安装 cygwin,这是一个在Windows平台上模拟运行Unix的环境,Nutch要在这里运行. 默认安装到c:cygwin目录,桌面生成一个Cygwin的图标
4.到http://www.nutch.org下载Nutch 1.2。解压缩得到nutch-1.2文件夹。把整个文件夹放到c:cygwin目录下
5.测试,运行Cygwin,出现黑白的控制台界面,像dos一样,
输入 cd /nutch-1.2进入刚才拷贝进去的nutch
输入 bin/nutch 执行bin/nutch,
看到下列提示,则说明安装成功:
Usage: nutch [-core] COMMAND
where COMMAND is one of :
......
休息一会儿,再继续,要不你就晕了。
6. 设置待抓取网站
在nucth-1.2目录下建立urls目录(也可以自己命名),在urls目录下建立个文本文件,名称无所谓,我取名为nutch,没有扩展名,扩展名为.txt也可以
打开刚才建立的这个名称为nutch的文件,输入待抓取的网站地址,如:
http://www.isosee.com/
http://www.pmpway.com/
最后的/不能漏掉.
编辑conf目录下的crawl-urlfilter.txt文件,该文件用于设置爬虫的过滤条件
# accept hosts in MY.DOMAIN.NAME
+^http://([a-z0-9]*.)*MY.DOMAIN.NAME/
修改为:
# accept hosts in MY.DOMAIN.NAME
+^http://([a-z0-9]*.)*www.isosee.com/
+^http://([a-z0-9]*.)*www.pmpway.com/
表示抓取域名下的所有页面
编辑conf目录下的nutch-site.xml文件,该文件用于将爬虫信息告诉被抓取的网站,如果不进行设置nutch不能运行.
该文件默认为这样:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<!-- Put site-specific property overrides in this file. -->
<configuration>
</configuration>
下面是我修改后的一个例子:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<!-- Put site-specific property overrides in this file. -->
<configuration>
<property>
<name>http.agent.name</name>
<value>myfirsttest</value>
<description>HTTP 'User-Agent' request header. MUST NOT be empty -
please set this to a single word uniquely related to your organization.
NOTE: You should also check other related properties:
http.robots.agents
http.agent.description
http.agent.url
http.agent.email
http.agent.version
and set their values appropriately.
</description>
</property>
<property>
<name>http.agent.description</name>
<value>myfirsttest</value>
<description>Further description of our bot- this text is used in
the User-Agent header. It appears in parenthesis after the agent name.
</description>
</property>
<property>
<name>http.agent.url</name>
<value>myfirsttest.com</value>
<description>A URL to advertise in the User-Agent header. This will
appear in parenthesis after the agent name. Custom dictates that this
should be a URL of a page explaining the purpose and behavior of this
crawler.
</description>
</property>
<property>
<name>http.agent.email</name>
<value>test@test.com</value>
<description>An email address to advertise in the HTTP 'From' request
header and User-Agent header. A good practice is to mangle this
address (e.g. 'info at example dot com') to avoid spamming.
</description>
</property>
</configuration>
上述文件描述了爬虫的名称/描述/来自哪个网站/联系email等信息.
好了,还等什么,开始抓取网页吧
在cygwin中执行抓取并建立索引的命令:
bin/nutch crawl urls -dir isosee -depth 4 -threads 5 -topN 1000 >&logs/log1.log
其中
urls目录中的文件里记录了待爬网站地址
-dir指定爬来的信息放到哪个目录下
-depth 指定抓取的深度
-thread 指定线程数
-topN 指定抓取该网站的前若干页,这个参数对于抓取大网站的网页非常有用
>&logs/log1.log指定日志存放位置,如果你想在控制台(也就是Cygwin的黑白窗口里)监视运行情况,可以不使用这行代码。如果使用的话别忘了事先在nutch-1.2目录下建个logs目录。
......等待,抓取及建立索引结束。
抓取和索引结果存放在isosee目录下
提示:先抓小网站试试,否则后果不堪设想,我抓取过我的另一个网站http://www.isosee.com,虽然只有1000个左右的页面,但用了将近1小时,当然实际时间跟你的机器配置、网络连接情况以及对方网站的关系都很大。
7.使用抓取和索引的成果建立搜索网站
配置tomcat
删除或改名Tomcat/webapps下的ROOT文件夹。
复制nutch-1.2.war到到tomcat的webapps/下,同时改名为ROOT.war(Tomcat运行时将自动解开为ROOT目录)
修改/webapps/ROOT/WEB-INF/classes/nutch-site.xml :
将
<configuration>
</configuration>
换成
<configuration>
<property>
<name>searcher.dir</name>
<value>C:cygwinnutch-1.2isosee</value>
</property>
</configuration>
把value里的内容替换为你实际存放抓取内容的地址,注意最后没有/,我开始加了/好像不行.
为了支持中文需要修改tomcat的配置文件,打开tomcat/conf下的server.xml文件,将其中的Connector部分改成如下形式即可:
<Connector port="8080" maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false"
redirectPort="8443"
acceptCount="100"
connectionTimeout="20000"
disableUploadTimeout="true"
URIEncoding="UTF-8" useBodyEncodingForURI="true" />
注意最后一行的两项是新加的.
8.利用搜索你自己的网站
重启tomcat,在浏览器中输入:http://127.0.0.1:8080
![[转载]Windows下开源搜索引擎Nutch1.2的安装 [转载]Windows下开源搜索引擎Nutch1.2的安装](https://i-blog.csdnimg.cn/blog_migrate/a4c26d1e5885305701be709a3d33442f.gif)
出现nutch搜索界面, 好了,自己的搜索引擎已经建立好了,跟google和百度一样。
在搜索框中输入关键词并搜索,将看到你的搜索结果
当然,某些搜索结果页可能出现中文乱码等问题,这跟Nutch关系不大,后期慢慢调整TOMCAT的配置即可。
总结与建议:Nutch是非常棒的基于Lucene的开源搜索引擎,Lucene为Nutch提供了文本索引和搜索的API。它的开放性架构以及优良的性能为我们学习和进一步开发提供了坚实的基础,甚至是搜索引擎架构从业者必然的选择。