Apache Nutch

Nutch 当前两个版本 :

  • 1.6 - Nutch1.6使用Hadoop Distributed File System (HDFS)来作为存储,稳定可靠。
  • 2.1 - 通过gora对存储层进行了扩展,可以选择使用HBase、Accumulo、Cassandra 、MySQL 、DataFileAvroStore、AvroStore中任何一种来存储数据,但其中一些并不成熟。

在Linux(Centos)上搭建 Nutch 框架:

  1. 安装 svn
    yum install subversion
  2. 安装 ant
    yum install ant
  3. check out nutch(进入 http://nutch.apache.org ,在 Version Control 板块可查看到svn地址。)
    svn co https://svn.apache.org/repos/asf/nutch/tags/release-1.6/
  4. ant 构建 nutch
    cd release-1.6/
    ant

ant 构建完成之后,在 release-1.6 目录下生成两个目录 :build、runtime,进入 runtime ,有两个子文件夹 :deploy、local,分别代表了nutch两种运行方式 :

  • deploy - hadoop 运行
  • local - 本地文件系统运行,只能有一个Map和Reduce。

local/bin/nutch :分析nutch脚本是入门的重点,可以看到通过 nutch 脚本连接Hadoop与Nutch,把apache-nutch-1.6.job提交给Hadoop的JobTracker;同时也可以看到在命令中所指定的是哪个Java类。

Nutch 脚本

nutch 的所有参数

复制代码
[root@localhost local]# bin/nutch 
Usage: nutch COMMAND
where COMMAND is one of:
  crawl             one-step crawler for intranets (DEPRECATED - USE CRAWL SCRIPT INSTEAD)
  readdb            read / dump crawl db
  mergedb           merge crawldb-s, with optional filtering
  readlinkdb        read / dump link db
  inject            inject new urls into the database
  generate          generate new segments to fetch from crawl db
  freegen           generate new segments to fetch from text files
  fetch             fetch a segment's pages
  parse             parse a segment's pages
  readseg           read / dump segment data
  mergesegs         merge several segments, with optional filtering and slicing
  updatedb          update crawl db from segments after fetching
  invertlinks       create a linkdb from parsed segments
  mergelinkdb       merge linkdb-s, with optional filtering
  solrindex         run the solr indexer on parsed segments and linkdb
  solrdedup         remove duplicates from solr
  solrclean         remove HTTP 301 and 404 documents from solr
  parsechecker      check the parser for a given url
  indexchecker      check the indexing filters for a given url
  domainstats       calculate domain statistics from crawldb
  webgraph          generate a web graph from existing segments
  linkrank          run a link analysis program on the generated web graph
  scoreupdater      updates the crawldb with linkrank scores
  nodedumper        dumps the web graph's node scores
  plugin            load a plugin and run one of its classes main()
  junit             runs the given JUnit test
 or
  CLASSNAME         run the class named CLASSNAME
Most commands print help when invoked w/o parameters.
复制代码
[root@localhost local]# bin/nutch crawl
Usage: Crawl <urlDir> -solr <solrURL> [-dir d] [-threads n] [-depth i] [-topN N]

参数的意义:

  • urlDir - 种子url的目录地址
  • -solr - <solrUrl>为solr的地址(如果没有则为空)
  • -dir - 保存爬取文件的目录
  • -threads - 爬取线程数量(默认10)
  • -depth - 爬取深度 (默认5)
  • -topN - 访问的广度 (默认是Long.max)

配置 local/conf/nutch-site.xml

Nutch 的提高在于研读nutch-default.xml中每一个配置的实际含义,需要结合源代码理解。打开 local/conf/nutch-default.xml,找到 :

复制代码
<property>
  <name>http.agent.name</name>
  <value></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>
复制代码

将以上配置复制到 nutch-site.xml 的 <configuration></configuration> 中,http.agent.name 的value值(<value></value>)是基于浏览器的User-Agent( 用户代理 ),它是一个特殊字符串头,使得服务器能够识别客户使用的操作系统及版本、CPU 类型、浏览器及版本、浏览器渲染引擎、浏览器语言、浏览器插件等,如:Opera/9.80 (Windows NT 5.1; Edition IBIS) Presto/2.12.388 Version/12.15。这个是Nutch服从Robot协议,所以要改。

添加种子url

在local目录下建文件夹如urls,在urls里面建文件如url,里面加入你要爬取的网站的入口url,如 :http://www.163.com/

配置local/conf/regex-urlfilter.txt

打开local/conf/regex-urlfilter.txt,注释掉最后一行,并添上你要抓取的网站的域名 :

# skip URLs with slash-delimited segment that repeats 3+ times, to break loops
-.*(/[^/]+)/[^/]+\1/[^/]+\1/

# accept anything else
# +.
+^http://([a-z0-9]*\.)*163\.com/

现在就可以爬取163所有的网页了, 在local目录下新建文件夹data,保存爬取内容,选择合适的参数:

nohup bin/nutch crawl urls -dir data &

nohup 命令将把输出的信息附加到的 nohup.out 文件中;在执行 nutch 会把爬虫的记录生成到 local/logs/hadoop.log

在爬取完成后,在 data 的文件夹下会有三个文件夹crawldb、linkdb、segments :

  • crawldb - 是所有需要爬取的超链接
  • Linkdb - 存放的是所有超连接及其每个连接的链入地址和锚文本
  • segments - 存放的是抓取的页面,以爬取的时间命名,个数不多于爬取的深度,Nutch的爬取策略是广度优先,每一层url生成一个文件夹,直到没有新的url。

在segments有6个文件夹 :

  • crawl_generate - names a set of urls to be fetched(待爬取的url)
  • crawl_fetch - contains the status of fetching each url(爬取的url的状态)
  • content - contains the content of each url(页面内容)
  • parse_text - contains the parsed text of each url(网页的文本信息)
  • parse_data - contains outlinks and metadata parsed from each url(url解析出来的外链和元数据)
  • crawl_parse - contains the outlink urls, used to update the crawldb(更新crawldb的外链)

这些文件夹都是不可读的,以方便存取并在高一层进行检索用。如果想看到具体内容,要使用Nutch定义的读取命令 :

1、查看CrawlDB(readdb)

复制代码
[root@localhost local]# bin/nutch readdb
Usage: CrawlDbReader <crawldb> (-stats | -dump <out_dir> | -topN <nnnn> <out_dir> [<min>] | -url <url>)
    <crawldb>    directory name where crawldb is located
    -stats [-sort]     print overall statistics to System.out
        [-sort]    list status sorted by host
    -dump <out_dir> [-format normal|csv|crawldb]    dump the whole db to a text file in <out_dir>
        [-format csv]    dump in Csv format
        [-format normal]    dump in standard format (default option)
        [-format crawldb]    dump as CrawlDB
        [-regex <expr>]    filter records with expression
        [-status <status>]    filter records by CrawlDatum status
    -url <url>    print information on <url> to System.out
    -topN <nnnn> <out_dir> [<min>]    dump top <nnnn> urls sorted by score to <out_dir>
        [<min>]    skip records with scores below this value.
            This can significantly improve performance.
复制代码

查看URL地址总数和它的状态及评分 :

复制代码
[root@localhost local]# bin/nutch readdb data/crawldb/ -stats
CrawlDb statistics start: data/crawldb/
Statistics for CrawlDb: data/crawldb/
TOTAL urls:    10635
retry 0:    10615
retry 1:    20
min score:    0.0
avg score:    2.6920545E-4
max score:    1.123
status 1 (db_unfetched):    9614
status 2 (db_fetched):    934
status 3 (db_gone):    2
status 4 (db_redir_temp):    81
status 5 (db_redir_perm):    4
CrawlDb statistics: done
复制代码

 导出每个url地址的详细内容:bin/nutch readdb data/crawldb/ -dump crawldb(导出的地址)

2、查看linkdb

查看链接情况:bin/nutch readlinkdb data/linkdb/ -url http://www.163.com/
导出linkdb数据库文件:bin/nutch readlinkdb 163/linkdb/ -dump linkdb(导出的地址)

3、查看segments

bin/nutch readseg -list -dir data/segments/  -  可以看到每一个segments的名称,产生的页面数,抓取的开始时间和结束时间,抓取数和解析数。

[root@localhost local]# bin/nutch readseg -list -dir data/segments/
NAME              GENERATED    FETCHER START          FETCHER END            FETCHED    PARSED
20130427150144    53           2013-04-27T15:01:52    2013-04-27T15:05:15    53         51
20130427150553    1036         2013-04-27T15:06:01    2013-04-27T15:58:09    1094       921
20130427150102    1            2013-04-27T15:01:10    2013-04-27T15:01:10    1          1

导出segments :bin/nutch readseg -dump data/segments/20130427150144 segdb
其中data/segments/20130427150144 为一个segments文件夹,segdb为存放转换后的内容的文件夹。

最后一个命令可能是最有用的,用于获得页面内容,一般会加上几个选项 
bin/nutch readseg -dump  data/segments/20130427150144/ data_oscar /segments -nofetch -nogenerate -noparse -noparsedata -nocontent
这样得到的 dump文件只包含网页的正文信息,没有标记。


--------------------------------------------------------------------------------------------------------------------------------------------------------

Nutch Crawler工作流程:

  1. Injector - 注入
  2. Generator - 产生抓取列表
  3. Fetcher - 从网上抓取网页
  4. Parse Segment - 对抓取的网页进行解析
  5. CrawlDB Update - 把抓取的URL状态和新发现的URL存入Crawl DB
  6. LinkDB invertlinks

在 2-5 步骤循环 topN 次,最后执行 5步骤。

 

 

 

分类:  Apache Nutch


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<p>Nutch的创始人是Doug Cutting,他同时也是Lucene、Hadoop和Avro开源项目的创始人。</p><p>Nutch诞生于2002年8月,是Apache旗下的一个用Java实现的开源搜索引擎项目,自Nutch1.2版本之后,Nutch已经从搜索引擎演化为网络爬虫,接着Nutch进一步演化为两大分支版本:1.X和2.X,这两大分支最大的区别在于2.X对底层的数据存储进行了抽象以支持各种底层存储技术。</p><p>在Nutch的进化过程中,产生了Hadoop、Tika、Gora和Crawler Commons四个Java开源项目。如今这四个项目都发展迅速,极其火爆,尤其是Hadoop,其已成为大规模数据处理的事实上的标准。Tika使用多种现有的开源内容解析项目来实现从多种格式的文件中提取元数据和结构化文本,Gora支持把大数据持久化到多种存储实现,Crawler Commons是一个通用的网络爬虫组件。</p><p>大数据这个术语最早的引用可追溯到Nutch。当时,大数据用来描述为更新网络搜索索引需要同时进行批量处理或分析的大量数据集。现在,大数据的含义已经被极大地发展了,业界将大数据的特性归纳为4个“V”。Volume数据体量巨大,Variety数据类型繁多,Value价值密度低,商业价值高,Velocity处理速度快。</p><p>Hadoop是大数据的核心技术之一,而Nutch集Hadoop之大成,是Hadoop的源头。学习Hadoop,没有数据怎么办?用Nutch抓!学了Hadoop的Map Reduce以及HDFS,没有实用案例怎么办?学习NutchNutch的很多代码是用Map Reduce和HDFS写的,哪里还能找到比Nutch更好的Hadoop应用案例呢?</p>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值