【干货】.NET开发通用组件发布(三) 简易数据采集组件

时间:2022-05-27 19:33:21

组件介绍和合作开发

  http://www.cnblogs.com/MrHuo/p/MrHuoControls.html

简易数据采集组件

怎么说他是一个简易的数据采集组件呢?因为由于时间仓促,缺少从某位置开始到某位置结束这种模式的采集,暂且叫他简易数据采集组件吧。

直接进入主题。

准备

引用:MrHuo.Controls.Gather;

测试效果

1、我的测试代码是这样的:

using MrHuo.Controls.Gather;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Gather gather = new Gather()
            {
                RegexPattern = @"(?is)<a[^>]*?href=(['""]?)(?<url>[^'""\s>]+)\1[^>]*>(?<text>(?:(?!</?a\b).)*)</a>",
                Url = "http://www.mrhuo.com"
            };
            gather.OnBeginCollect += gather_OnBeginCollect;
            gather.OnCollecting += gather_OnCollecting;
            gather.OnEndCollect += gather_OnEndCollect;
            gather.OnError += gather_OnError;
            gather.Collect();

            Console.ReadLine();
        }

        static void gather_OnError(Exception obj)
        {
            Console.WriteLine("采集过程中发生错误:" + obj.Message);
        }

        static void gather_OnEndCollect()
        {
            Console.WriteLine("采集结束.");
        }

        static void gather_OnCollecting(System.Text.RegularExpressions.Match obj)
        {
            Console.WriteLine("正在采集:" + obj.Groups["text"].Value + "(" + obj.Groups["url"].Value + ")");
        }

        static void gather_OnBeginCollect()
        {
            Console.WriteLine("采集开始...");
        }
    }
}

2、采集结果:

【干货】.NET开发通用组件发布(三) 简易数据采集组件

3、关于采集过程中为什么不一次性输出结果,暂时没有更好的解决方法,所以暂无提供。

如有好的想法和建议,可以发送电子邮件到:admin@mrhuo.com,参与项目开发。

测试项目下载:

点击下载