Nutch 初体验 爬行企业内部网(转载)|后台服务器|码途山海.智隐长卷 -

程序人生|重庆纽新

找回密码
立即注册

QQ登录

只需一步,快速开始

欢迎访问【程序人生-重庆纽新】,本网站为软件开发人员视觉的IT资讯、软件开发中各种问题的解决办法!!
搜索
发新帖


2308

积分

0

好友

259

主题
楼主
发表于 2017-10-14 16:44:59 | 查看: 1460| 回复: 0
前些日子琢磨着想搭建一个搜索引擎,自己写成本有点高,虽然以前写过爬虫,但是索引排序估计要烦得多
nutch   是一个开源的、Java 实现的搜索引擎。它提供了我们运行自己的搜索引擎所需的全部工具。是一个应用程序,可以以 Lucene 为基础实现搜索引擎应用。
选定nutch之后,开始着手学习使用nutch,英文水平还不够,只能看看nutch的简单的tutorial,但是真正当教程,我还是选择了中文,可以让第一个搜索跑起来之后再选择学习英文的文档,以便更深的理解。
我选择的教程是 nutch入门学习
准备工作:
我的系统是Ubuntu 9.10,java -version 1.6.0_20-b02,nutch 1.0,以及tomcat 6.0.26
  • jdk和tomcat一般大家做过java和web开发都会有装,不赘述,有几点需要注意的列出来
    • tomcat的bin/catalina.sh中加入JAVA_HOME=/usr/lib/jvm/java-6-sun-1.6.0.20,这点我深受其害,开始没有设置,运行bin/nutch crawl的时候总是说JAVA_HOME is not set,我一想我明明设置了java环境变量的,java-version也是正常的,各种google,确定各种地方可以设置JAVA_HOME的地方,都无济于事,最后在一个角落找到,在此文件中可以添加JAVA_HOME,然后运行,居然可以,但是我不明白,nutch爬虫的运行应该是不依赖于tomcat的,tomcat只是用于搜索。这点未参透。
  • tomcat,jdk搞定之后是nutch,我直接将nutch放在用户名下面的nutch目录,然后将其中的nutch.war复制到tomcat的webapp中,并取代ROOT(解压,重命名目录)

配置nutch:
这里参考nutch入门 学习,我把改的地方说明出来。

  • 增加要抓取的页面(以www.163.com为例)
  • 编辑conf/crawl-urlfilter.txt文件,设定要抓取的网址信息。
    [root@localhost nutch]#vi conf/crawl-urlfilter.txt
    修改MY.DOMAIN.NAME为:
    # accept hosts in MY.DOMAIN.NAME
    +^http://([a-z0-9]*.)*163.com/
  • 编辑conf/nutch-site.xml文件,增加代理的属性,并编辑相应的属性值
    Xml代码  [url=] [/url]
    •   
    • http.agent.name  
    •   
    • 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.  
    •   
    •   
    •   
    • http.agent.description  
    •   
    • Further description of our bot- this text is used in  
    • the User-Agent header. It appears in parenthesis after the agent  
    • name.  
    •   
    •   
    •   
    • http.agent.url  
    •   
    • 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.  
    •   
    •   
    •   
    • http.agent.email  
    •   
    • 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.  
    •   

    nutch入门 学习中说这里就算是不修改也无所谓,这里的设置,是因为nutch遵守了robots协议,在获取response时,把自己的相关信息提交给被爬行的网站,以供识别。但是我这样设置出现了错误提示,即http.agent.name需要设置,我将value设置成 xusulong*(记住有*)即可。其他可以不设置了。
配置tomcat:
  • 设定搜索目录
    (是由于默认的segment路径与我们实际的路径不符所造成的)
    [root@localhost nutch]#cd ~/tomcat
    [root@localhost tomcat]#vi webapps/ROOT/WEB-INF/classes/nutch-site.xml
    增加四行代码,修改成为
    Xml代码  [url=][/url]
    •   
    •   
    • searcher.dir  
    • /home/whu/nutch/crawl.demo  
    •   
    •   

    这里的/home/whu/nutch/crawl.demo是我的nutch路径,爬虫到时候的数据就会放在程序新建的crawl.demo下面,即nutch抓取的页面的保存目录。
  • nutch对中文的支持还不完善,需要修改tomcat文件夹下conf/server.xml文件
    [root@localhost tomcat]#vi conf/server.xml
    增加两句,修改为
    <connector port="8080"
    maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
    enableLookups="false" redirectPort="8443" acceptCount="100"
    connectionTimeout="20000" disableUploadTimeout="true"
    URIEncoding="UTF-8" useBodyEncodingForURI="true" />
抓取网页:
whu@leopard:~/nutch$ bin/nutch crawl urls -dir crawl.demo -depth 2 -threads 4 -topN 5 >& crawl.log

具体的参数nutch入门 学习 有解释,也可以参见nutch的官方网站。这里只抓取少量站点。
这时候 crawl.log会记录抓取的信息,我中间遇到过
如下几个错误:
  • http.agent.name需要设置问题
  • Input path does not exist问题,这个多试几次路径即可,只要这里的crawl.demo和配置tomcat中的路径对应,记得出错的时候把出错的目录删除,否则下次还是出错。
测试结果:
运行tomcat,进入首页,搜索网易,结果如下:


搞了一个下午和晚上,泪流满面,中途还有其他的错误我记不大清楚了,总之严重的错误我列出来了,仔细看系统如何报错,google之,仔细发现错误才是王道。

收藏回复 只看该作者 道具 举报

高级模式
B Color Image Link Quote Code Smilies



QQ|小黑屋| 码途山海.智隐长卷 渝ICP备15002301号-2   渝公网安备50011202504426

GMT+8, 2025-5-18 04:42 , Processed in 0.062014 second(s), 26 queries .

©Copyright 程序人生!

©2012-2015重庆纽新

快速回复 返回顶部 返回列表