今天我们看看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页面的。

转载于:https://www.cnblogs.com/Anfield/p/3929390.html

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

  1. python爬虫怎么爬同一个网站的多页数据-如何用Python爬数据?(一)网页抓取

    如何用Python爬数据?(一)网页抓取 你期待已久的Python网络数据爬虫教程来了.本文为你演示如何从网页里找到感兴趣的链接和说明文字,抓取并存储到Excel. 需求 我在公众号后台,经常可以收到 ...

  2. python爬网站数据实例-如何用Python爬数据?(一)网页抓取

    如何用Python爬数据?(一)网页抓取 你期待已久的Python网络数据爬虫教程来了.本文为你演示如何从网页里找到感兴趣的链接和说明文字,抓取并存储到Excel. 需求 我在公众号后台,经常可以收到 ...

  3. vs用Python爬数据?(一)网页抓取

    你期待已久的Python网络数据爬虫教程来了.本文为你演示如何从网页里找到感兴趣的链接和说明文字,抓取并存储到Excel. (由于微信公众号外部链接的限制,文中的部分链接可能无法正确打开.如有需要,请 ...

  4. 用Python构建网页抓取器

    借助使用Python构建的尖端网页抓取技术,启动您的大数据项目 Scrape the Planet! Building Web Scrapers with Python 你会学到什么 如何理论化和开发 ...

  5. 代理服务器ip地址如何获得_详细教程:如何使用代理服务器进行网页抓取?

    全文共2136字,预计学习时长7分钟 图源:Unsplash 万维网是数据的宝库.大数据的易得性.数据分析软件的迅猛发展以及日益廉价的计算能力进一步提高了数据驱动战略对竞争差异化的重要性. 据Forr ...

  6. 实现织梦dedecms百度主动推送(实时)网页抓取

    做百度推广的时候,如何让百度快速收录呢,下面提供了三种方式,今天我们主要讲的是第一种. 如何选择链接提交方式 1.主动推送:最为快速的提交方式,推荐您将站点当天新产出链接立即通过此方式推送给百度,以保 ...

  7. Java 网络实例二(查看主机指定文件的最后修改时间、Socket实现多线程服务器程序、Socket连接到指定主机、网页抓取)

    查看主机指定文件的最后修改时间 import java.net.URL; import java.net.URLConnection; import java.util.Date; import ja ...

  8. ip代理服务器软件25探索云速捷_使用代理进行Web网页抓取的基础

    该全球网络是数据的宝库.大数据的可用性,数据分析软件的迅猛发展以及日益廉价的计算能力,进一步提高了数据驱动型战略对竞争差异化的重要性. 根据Forrester的报告,数据驱动型公司利用其整个组织的洞察 ...

  9. asp.net 获取全部在线用户_提取在线数据的9个最佳网页抓取工具

    Web Scraping工具专门用于从网站中提取信息.它们也被称为网络收集工具或Web数据提取工具. Web Scraping工具可以在各种场景中用于无限目的. 比如: 1.收集市场研究数据 网络抓取 ...

  10. 系统检测到您疑似使用网页抓取工具访问本_12款最常使用的网络爬虫工具推荐...

    网络爬虫在当今的许多领域得到广泛应用.它的作用是从任何网站获取特定的或更新的数据并存储下来.网络爬虫工具越来越为人所熟知,因为网络爬虫简化并自动化了整个爬取过程,使每个人都可以轻松访问网站数据资源.使 ...

最新文章

  1. 2张图简单分析count(0)与count(*)
  2. ASP.NET页面指令
  3. 【小练习04】HTML+CSS--医药健康小页面
  4. netcore dapper mysql_.NET Core Dapper操作mysql数据库
  5. Comparable接口和Comparator接口的比较
  6. Elasticsearch搜索引擎之缓存:Request Cache、Query Cache、Fielddata Cache
  7. Js中 call() 与 apply() exports
  8. oracle空例程,2018.5.29 Oracle连接到空闲例程
  9. candence pcb走线等长_Allegro的通用等长规则设置方法
  10. sql按年、月、日、查询排序
  11. Machine Learning——Homework1
  12. Linux有问必答:如何查看Linux上程序或进程用到的库
  13. c语言的编程特点,c语言编程是什么?C语言编程的特点和应用
  14. HPSocket 三种模型PACK,PUSH,PULL
  15. 人工智能——单层感知器
  16. winform中rdlc报表配置
  17. 基于鸿蒙系统的APP测试技术,快来入坑
  18. GitHub热门项目 | PyTorch 资源大全,7400多星
  19. SFTP下载文件到本地
  20. 你怎么看待互联网创业的国外问卷调查?

热门文章

  1. env-FindClass()为NULL的一种解决办法
  2. 编译opencv错误解决:libavcodec.a(hevc_cabac.o): `ff_h264_cabac_tables' can not be used
  3. pca主成分分析_PCA主成分分析(中)
  4. 吴军信息论40讲_刘润对谈吴军:每个人都一定要有数学思维
  5. java多线程volatile_java多线程——volatile
  6. php 文件预览 水印,php实现在线预览word等office文件,同时添加水印
  7. php7会带领,php7中不能做的10件事
  8. Mybatis学习(2)—— 对象关系映射
  9. 大数据踩坑之旅: 从数据可视化到商业智能
  10. DaRT启动光盘使用详解