【转】Nutch源代码研究 网页抓取 数据结构

今天我们看看Nutch网页抓取,所用的几种数据结构: 
主要涉及到了这几个类:FetchListEntry,Page, 
首先我们看看FetchListEntry类: 
public final class FetchListEntry implements Writable, Cloneable 
实现了Writable, Cloneable接口,Nutch许多类实现了Writable, Cloneable。 
自己负责自己的读写操作其实是个很合理的设计方法,分离出来反倒有很琐碎 
的感觉。

看看里面的成员变量:

1 public static final String DIR_NAME = "fetchlist";//要写入磁盘的目录
2 private final static byte CUR_VERSION = 2;//当前的版本号
3 private boolean fetch;//是否抓取以便以后更新
4 private Page page;//当前抓取的页面
5 private String[] anchors;//抓取到的该页面包含的链接

我们看看如何读取各个字段的,也就是函数 
public final void readFields(DataInput in) throws IOException 
读取version 字段,并判断如果版本号是否大约当前的版本号,则抛出版本不匹配的异常, 
然后读取fetch 和page 字段。 
判断如果版本号大于1,说明anchors已经保存过了,读取anchors,否则直接赋值一个空的字符串 
代码如下:

 1     byte version = in.readByte();                 // read version
 2     if (version > CUR_VERSION)                    // check version
 3       throw new VersionMismatchException(CUR_VERSION, version);
 4
 5     fetch = in.readByte() != 0;                   // read fetch flag
 6
 7     page = Page.read(in);                         // read page
 8
 9     if (version > 1) {                            // anchors added in version 2
10       anchors = new String[in.readInt()];         // read anchors
11       for (int i = 0; i < anchors.length; i++) {
12         anchors[i] = UTF8.readString(in);
13       }
14     } else {
15       anchors = new String[0];
16     }
17  

同时还提供了一个静态的读取各个字段的函数,并构建出FetchListEntry对象返回:

1 public static FetchListEntry read(DataInput in) throws IOException {
2     FetchListEntry result = new FetchListEntry();
3     result.readFields(in);
4     return result;
5 }

写得代码则比较易看,分别写每个字段:

1 public final void write(DataOutput out) throws IOException {
2     out.writeByte(CUR_VERSION);                   // store current version
3     out.writeByte((byte)(fetch ? 1 : 0));         // write fetch flag
4     page.write(out);                              // write page
5     out.writeInt(anchors.length);                 // write anchors
6     for (int i = 0; i < anchors.length; i++) {
7       UTF8.writeString(out, anchors[i]);
8     }
9   }

其他的clone和equals函数实现的也非常易懂。 
下面我们看看Page类的代码: 
public class Page implements WritableComparable, Cloneable 
和FetchListEntry一样同样实现了Writable, Cloneable接口,我们看看Nutch的注释,我们就非常容易知道各个字段的意义了:

/*********************************************
 * A row in the Page Database.
 * <pre>
 *   type   name    description
 * ---------------------------------------------------------------
 *   byte   VERSION  - A byte indicating the version of this entry.
 *   String URL      - The url of a page.  This is the primary key.
 *   128bit ID       - The MD5 hash of the contents of the page.
 *   64bit  DATE     - The date this page should be refetched.
 *   byte   RETRIES  - The number of times we‘ve failed to fetch this page.
 *   byte   INTERVAL - Frequency, in days, this page should be refreshed.
 *   float  SCORE   - Multiplied into the score for hits on this page.
 *   float  NEXTSCORE   - Multiplied into the score for hits on this page.
 * </pre>
 *
 * @author Mike Cafarella
 * @author Doug Cutting
 *********************************************/

各个字段:

 1 private final static byte CUR_VERSION = 4;
 2   private static final byte DEFAULT_INTERVAL =
 3     (byte)NutchConf.get().getInt("db.default.fetch.interval", 30);
 4
 5   private UTF8 url;
 6   private MD5Hash md5;
 7   private long nextFetch = System.currentTimeMillis();
 8   private byte retries;
 9   private byte fetchInterval = DEFAULT_INTERVAL;
10   private int numOutlinks;
11   private float score = 1.0f;
12   private float nextScore = 1.0f;

同样看看他是如何读取自己的各个字段的,其实代码加上本来提供的注释,使很容易看懂的,不再详述:

 1 ublic void readFields(DataInput in) throws IOException {
 2     byte version = in.readByte();                 // read version
 3     if (version > CUR_VERSION)                    // check version
 4       throw new VersionMismatchException(CUR_VERSION, version);
 5
 6     url.readFields(in);
 7     md5.readFields(in);
 8     nextFetch = in.readLong();
 9     retries = in.readByte();
10     fetchInterval = in.readByte();
11     numOutlinks = (version > 2) ? in.readInt() : 0; // added in Version 3
12     score = (version>1) ? in.readFloat() : 1.0f;  // score added in version 2
13     nextScore = (version>3) ? in.readFloat() : 1.0f;  // 2nd score added in V4
14   }

写各个字段也很直接:

 1 public void write(DataOutput out) throws IOException {
 2     out.writeByte(CUR_VERSION);                   // store current version
 3     url.write(out);
 4     md5.write(out);
 5     out.writeLong(nextFetch);
 6     out.write(retries);
 7     out.write(fetchInterval);
 8     out.writeInt(numOutlinks);
 9     out.writeFloat(score);
10     out.writeFloat(nextScore);
11   }

我们顺便看看提供方便读写Fetch到的内容的类FetcherOutput:这个类通过委托前面介绍的两个类的读写,提供了Fetche到的各种粒度结构的读写功能,代码都比较直接,不再详述。

补充一下Content类:

public final class Content extends VersionedWritable 
我们看到继承了VersionedWritable类。VersionedWritable类实现了版本字段的读写功能。 
我们先看看成员变量:

1   public static final String DIR_NAME = "content";
2   private final static byte VERSION = 1;
3   private String url;
4   private String base;
5   private byte[] content;
6   private String contentType;
7   private Properties metadata;

DIR_NAME 为Content保存的目录, 
VERSION 为版本常量 
url为该Content所属页面的url 
base为该Content所属页面的base url 
contentType为该Content所属页面的contentType 
metadata为该Content所属页面的meta信息

下面我们看看Content是如何读写自身的字段的: 
public final void readFields(DataInput in) throws IOException 
这个方法功能为读取自身的各个字段

 1 super.readFields(in);                         // check version
 2
 3     url = UTF8.readString(in);                    // read url
 4     base = UTF8.readString(in);                   // read base
 5
 6     content = WritableUtils.readCompressedByteArray(in);
 7
 8     contentType = UTF8.readString(in);            // read contentType
 9
10     int propertyCount = in.readInt();             // read metadata
11     metadata = new Properties();
12     for (int i = 0; i < propertyCount; i++) {
13       metadata.put(UTF8.readString(in), UTF8.readString(in));
14     }

代码加注释之后基本上比较清晰了. 
super.readFields(in);        
这句调用父类VersionedWritable读取并验证版本号 
写的代码也比较简单:

 1 public final void write(DataOutput out) throws IOException {
 2     super.write(out);                             // write version
 3
 4     UTF8.writeString(out, url);                   // write url
 5     UTF8.writeString(out, base);                  // write base
 6
 7     WritableUtils.writeCompressedByteArray(out, content); // write content
 8
 9     UTF8.writeString(out, contentType);           // write contentType
10
11     out.writeInt(metadata.size());                // write metadata
12     Iterator i = metadata.entrySet().iterator();
13     while (i.hasNext()) {
14       Map.Entry e = (Map.Entry)i.next();
15       UTF8.writeString(out, (String)e.getKey());
16       UTF8.writeString(out, (String)e.getValue());
17     }
18   }

其实这些类主要是它的字段.以及怎样划分各个域模型的

下次我们看看parse-html插件,看看Nutch是如何提取html页面的。

时间: 2024-08-29 01:30:23

【转】Nutch源代码研究 网页抓取 数据结构的相关文章

【转】Nutch源代码研究 网页抓取 下载插件

今天我们来看看Nutch的源代码中的protocol-http插件,是如何抓取和下载web页面的.protocol-http就两个类HttpRespose和Http类,其中HttpRespose主要是向web服务器发请求来获取响应,从而下载页面.Http类则非常简单,其实可以说是HttpResponse的一个Facade,设置配置信息,然后创建HttpRespose.用户似乎只需要和Http类打交道就行了(我也没看全,所以只是猜测). 我们来看看HttpResponse类: 看这个类的源码需要从

基于Casperjs的网页抓取技术【抓取豆瓣信息网络爬虫实战示例】

CasperJS is a navigation scripting & testing utility for the PhantomJS (WebKit) and SlimerJS (Gecko) headless browsers, written in Javascript. PhantomJS是基于WebKit内核的headless browser SlimerJS则是基于Gecko内核的headless browser Headless browser: 无界面显示的浏览器,可以用于

用python做网页抓取与解析入门笔记[zz]

(from http://chentingpc.me/article/?id=961) 事情的起因是,我做survey的时候搜到了这两本书:Computational Social Network Analysis和Computational Social Network,感觉都蛮不错的,想下载下来看看,但是点开网页发现这个只能分章节下载,晕,我可没时间一章一章下载,想起了迅雷的下载全部链接,试试看,果真可以把他们一网打尽,但是,sadly,迅雷下载的时候,文件名没办法跟章节名对应起来,晕,我可

一个实用的C#网页抓取类代码分享

一个实用的C# 网页抓取类 模拟蜘蛛,类中定义了超多的C#采集文章.网页抓取文章的基础技巧,下面分享代码: using System; using System.Data; using System.Configuration; using System.Net; using System.IO; using System.Text; using System.Collections.Generic; using System.Text.RegularExpressions; using Sys

用Python进行网页抓取

引言 从网页中提取信息的需求日益剧增,其重要性也越来越明显.每隔几周,我自己就想要到网页上提取一些信息.比如上周我们考虑建立一个有关各种数据科学在线课程的欢迎程度和意见的索引.我们不仅需要找出新的课程,还要抓取对课程的评论,对它们进行总结后建立一些衡量指标.这是一个问题或产品,其功效更多地取决于网页抓取和信息提取(数据集)的技术,而非以往我们使用的数据汇总技术. 网页信息提取的方式 从网页中提取信息有一些方法.使用API可能被认为是从网站提取信息的最佳方法.几乎所有的大型网站,像Twitter.

网页抓取:PHP实现网页爬虫方式小结

来源:http://www.ido321.com/1158.html 抓取某一个网页中的内容,需要对DOM树进行解析,找到指定节点后,再抓取我们需要的内容,过程有点繁琐.LZ总结了几种常用的.易于实现的网页抓取方式,如果熟悉JQuery选择器,这几种框架会相当简单. 一.Ganon 项目地址: http://code.google.com/p/ganon/ 文档: http://code.google.com/p/ganon/w/list 测试:抓取我的网站首页所有class属性值是focus的

淘搜索之网页抓取系统分析与实现(2)—redis + scrapy

1.scrapy+redis使用 (1)应用 这里redis与scrapy一起,scrapy作为crawler,而redis作为scrapy的调度器.如架构图中的②所示.图1 架构图 (2)为什么选择redis redis作为调度器的实现仍然和其特性相关,可见<一淘搜索之网页抓取系统分析与实现(1)--redis使用>(http://blog.csdn.net/u012150179/article/details/38226711)中关于redis的分析. 2.redis实现scrapy sc

网页抓取

### -*- coding: cp936 -*-###<a href="http://home.51cto.com" target="_blank">家园</a>##import urllib##str0='<a href="http://home.51cto.com" target="_blank">家园</a>'##href=str0.find('<a href')#

网页抓取与处理的一些方法

昨天还是2014,今天就变成了2015.时间总是那么快,这篇文章就作为2015年的一个开始吧. 这篇文章主要介绍一些网页抓取及抓取下来的内容处理. 所需要的jar包点击打开链接,我放在百度云盘里.有需要的可以下载,其他的请自行下载. 百度百科对网页抓取的定义,当然本文并没有介绍的那么多,只是介绍对单个页面的抓取,和模拟提交表单抓取页面,如需深究,请自行baidu or google. 上面的方法直接返回String字符串,只需传入一个链接即可.相信大家都看的懂. 那么获取到的String字符串,