Nutch 当前两个版本 :
- 1.6 - Nutch1.6使用Hadoop Distributed File System (HDFS)来作为存储,稳定可靠。
- 2.1 - 通过gora对存储层进行了扩展,可以选择使用HBase、Accumulo、Cassandra 、MySQL 、DataFileAvroStore、AvroStore中任何一种来存储数据,但其中一些并不成熟。
在Linux(Centos)上搭建 Nutch 框架:
- 安装 svn
yum install subversion
- 安装 ant
yum install ant
- check out nutch(进入 http://nutch.apache.org ,在 Version Control 板块可查看到svn地址。)
svn co https://svn.apache.org/repos/asf/nutch/tags/release-1.6/
- 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类。
![](https://i-blog.csdnimg.cn/blog_migrate/cdec0645add3fc3c328197dda5c76203.gif)
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工作流程:
- Injector - 注入
- Generator - 产生抓取列表
- Fetcher - 从网上抓取网页
- Parse Segment - 对抓取的网页进行解析
- CrawlDB Update - 把抓取的URL状态和新发现的URL存入Crawl DB
- LinkDB invertlinks
在 2-5 步骤循环 topN 次,最后执行 5步骤。