Nutch介绍及使用

简介:

1. Nutch介绍

Nutch是一个开源的网络爬虫项目,更具体些是一个爬虫软件,可以直接用于抓取网页内容。

现在Nutch分为两个版本,1.x和2.x。1.x最新版本为1.7,2.x最新版本为2.2.1。两个版本的主要区别在于底层的存储不同。

1.x版本是基于Hadoop架构的,底层存储使用的是HDFS,而2.x通过使用Apache Gora,使得Nutch可以访问HBase、Accumulo、Cassandra、MySQL、DataFileAvroStore、AvroStore等NoSQL。

2. 编译Nutch

Nutch1.x从1.7版本开始不再提供完整的部署文件,只提供源代码文件及相关的build.xml文件,这就要求用户自己编译Nutch,而整个Nutch2.x版本都不提供编译完成的文件,所以想要学习Nutch2.2.1的功能,就必须自己手动编译文件。

2.1 下载解压

$ wget http://archive.apache.org/dist/nutch/2.2.1/apache-nutch-2.2.1-src.tar.gz
$ tar zxf apache-nutch-2.2.1-src.tar.gz

2.2 编译

$ cd apache-nutch-2.2.1
$ ant

有可能你会得到如下错误:

Trying to override old definition of task javac
  [taskdef] Could not load definitions from resource org/sonar/ant/antlib.xml. It could not be found.

ivy-probe-antlib:

ivy-download:
  [taskdef] Could not load definitions from resource org/sonar/ant/antlib.xml. It could not be found.

解决办法:

  1. 下载sonar-ant-task-2.1.jar,将其拷贝到apache-nutch-2.2.1目录下面
  2. 修改build.xml,引入上面添加的jar包:
<!-- Define the Sonar task if this hasn't been done in a common script -->
<taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
	<classpath path="${ant.library.dir}" />
	<classpath path="${mysql.library.dir}" />
	<classpath><fileset dir="." includes="sonar*.jar" /></classpath>
</taskdef>

Nutch使用ivy进行构建,故编译需要很长时间,如果编译时间过长,建议修改maven仓库地址,修改方法:

通过用http://mirrors.ibiblio.org/maven2/替换ivy/下ivysettings.xml中的http://repo1.maven.org/maven2/来解决。代码位置为:

<property name="repo.maven.org" value="http://repo1.maven.org/maven2/" override="false"/>

编译之后的目录如下:

  apache-nutch-2.2.1  tree -L 1
.
├── CHANGES.txt
├── LICENSE.txt
├── NOTICE.txt
├── README.txt
├── build
├── build.xml
├── conf
├── default.properties
├── docs
├── ivy
├── lib
├── runtime
├── sonar-ant-task-2.1.jar
└── src

7 directories, 7 files

可以看到编译之后多了两个目录:build和runtime

3. 修改配置文件

由于Nutch2.x版本存储采用Gora访问Cassandra、HBase、Accumulo、Avro等,需要在该文件中制定Gora属性,比如指定默认的存储方式gora.datastore.default= org.apache.gora.hbase.store.HBaseStore,该属性的值可以在nutch-default.xml中查找storage.data.store.class属性取得,在不做gora.properties文件修改的情况下,存储类为org.apache.gora.memory.store.MemStore,该类将数据存储在内存中,仅用于测试目的。

这里,将其存储方式改为HBase,请参考 http://wiki.apache.org/nutch/Nutch2Tutorial。

修改 conf/nutch-site.xml

<property>
  <name>storage.data.store.class</name>
  <value>org.apache.gora.hbase.store.HBaseStore</value>
  <description>Default class for storing data</description>
</property>

修改 ivy/ivy.xml

<!-- Uncomment this to use HBase as Gora backend. -->
<dependency org="org.apache.gora" name="gora-hbase" rev="0.3" conf="*->default" />

修改 conf/gora.properties,确保HBaseStore被设置为默认的存储,

gora.datastore.default=org.apache.gora.hbase.store.HBaseStore

因为这里用到了HBase,故还需要一个HBase环境,你可以使用Standalone模式搭建一个HBase环境,请参考 HBase Quick Start。需要说明的时,目前HBase的版本要求为 hbase-0.90.4。

4. 集成Solr

由于建索引的时候需要使用Solr,因此我们需要安装并启动一个Solr服务器。

4.1 下载,解压

$ wget http://mirrors.cnnic.cn/apache/lucene/solr/4.8.0/solr-4.8.0.tgz 
$ tar -zxf solr-4.8.0.tgz

4.2 运行Solr

$ cd solr-4.8.0/example
$ java -jar start.jar

验证是否启动成功

用浏览器打开 http://localhost:8983/solr/admin/,如果能看到页面,说明启动成功。

4.3 修改Solr配置文件

将apache-nutch-2.2.1/conf/schema-solr4.xml拷贝到solr-4.8.0/solr/collection1/conf/schema.xml,并在<fields>...</fields>最后添加一行:

<field name="_version_" type="long" indexed="true" stored="true" multiValued="false"/>

重启Solr,

# Ctrl+C to stop Solr
$ java -jar start.jar

5. 抓取数据

编译后的脚本在 runtime/local/bin 目录下,可以运行命令查看使用方法:

crawl命令:

$ cd runtime/local/bin 
$ ./crawl 
Missing seedDir : crawl <seedDir> <crawlID> <solrURL> <numberOfRounds>

nutch命令:

$ ./nutch 
Usage: nutch COMMAND
where COMMAND is one of:
 inject		inject new urls into the database
 hostinject     creates or updates an existing host table from a text file
 generate 	generate new batches to fetch from crawl db
 fetch 		fetch URLs marked during generate
 parse 		parse URLs marked during fetch
 updatedb 	update web table after parsing
 updatehostdb   update host table after parsing
 readdb 	read/dump records from page database
 readhostdb     display entries from the hostDB
 elasticindex   run the elasticsearch indexer
 solrindex 	run the solr indexer on parsed batches
 solrdedup 	remove duplicates from solr
 parsechecker   check the parser for a given url
 indexchecker   check the indexing filters for a given url
 plugin 	load a plugin and run one of its classes main()
 nutchserver    run a (local) Nutch server on a user defined port
 junit         	runs the given JUnit test
 or
 CLASSNAME 	run the class named CLASSNAME
Most commands print help when invoked w/o parameters.

接下来可以抓取网页了。

目录
相关文章
|
存储 人工智能 搜索推荐
Spring AI Alibaba DeepResearch源码解读
DeepResearch是SAA社区推出的智能体项目,支持复杂信息搜索、分析与结构化报告生成。其基于Graph构建14个协同节点(如Coordinator、Planner、Researcher等),融合Plan & Execute、LLM Reflection、Hybrid RAG、Self-evolving角色记忆、HITL等前沿技术,实现端到端深度研究自动化
1257 13
Spring AI Alibaba DeepResearch源码解读
|
7月前
|
人工智能 JSON Unix
langChain 快速入门
LangChain表达式语言(LCEL)以`|`管道符串联PromptTemplate、大模型与OutputParser三大组件,实现提示工程、推理与结果解析的标准化流水线。本文以翻译助手为例,详解如何用LCEL快速构建端到端可调用AI链,代码简洁、逻辑清晰。(239字)
|
JSON 安全 Java
API 一键转换 MCP 服务!Higress 助今日投资快速上线 MCP 市场
今日投资的技术负责人介绍了如何通过Higress MCP 市场完善的解决方案,快捷地将丰富的金融数据 API 转化为 MCP 工具,帮助用户通过 MCP 的方式非常轻松地调用专业金融数据,自由快速地构建自己的金融大模型应用。
1544 23
|
8月前
|
人工智能 弹性计算 机器人
OpenClaw到底是什么?阿里云3步一键部署,OpenClaw(原Moltbot)封神教程
OpenClaw(原Moltbot/Clawdbot)是阿里云推出的现代化零代码RPA平台,支持AI深度集成与持久记忆。本文详解其在阿里云的3种一键部署方式:计算巢、轻量应用服务器、无影云电脑/云手机,开箱即用,快速构建定制化AI助理。
933 1
|
传感器 数据采集 物联网
基于STM32和FreeRTOS的智能手环项目设计与实现【免费开源】
随着可穿戴设备的普及,智能手环逐渐成为健康管理、运动监测和生活便捷的重要工具。本项目旨在设计一款基于STM32微控制器和FreeRTOS实时操作系统的智能手环,具备心率监测、运动计步、睡眠分析以及蓝牙通信功能。通过FreeRTOS实现多任务调度,提高系统响应效率和资源利用率,同时保证低功耗设计,延长手环续航。
基于STM32和FreeRTOS的智能手环项目设计与实现【免费开源】
|
人工智能 数据可视化 计算机视觉
Ultralytics YOLO11来啦!更快!更强!
YOLO(You Only Look Once)是一种流行的物体检测和图像分割模型,由华盛顿大学的 Joseph Redmon 和 Ali Farhadi 开发。
Ultralytics YOLO11来啦!更快!更强!
|
存储 关系型数据库 分布式数据库
GraphRAG:基于PolarDB+通义千问+LangChain的知识图谱+大模型最佳实践
本文介绍了如何使用PolarDB、通义千问和LangChain搭建GraphRAG系统,结合知识图谱和向量检索提升问答质量。通过实例展示了单独使用向量检索和图检索的局限性,并通过图+向量联合搜索增强了问答准确性。PolarDB支持AGE图引擎和pgvector插件,实现图数据和向量数据的统一存储与检索,提升了RAG系统的性能和效果。
|
SQL XML JSON
大数据ETL开发之图解Kettle工具(入门到精通)
ETL (Extract-Transform-Load 的缩写,即数据抽取、转换、装载的过程),对于企业或行业应用来说,我们经常会遇到各种数据的处理,转换,迁移,所以了解并掌握一种ETL工具的使用,必不可少。
9350 0
大数据ETL开发之图解Kettle工具(入门到精通)
|
Java Python
【Python】已解决:ERROR: No matching distribution found for JPype
【Python】已解决:ERROR: No matching distribution found for JPype
1270 0

热门文章

最新文章