C#のクローラーライブラリ「Abot」

C#でWebサイトをクロールするライブラリの「Abot Web Crawler」というものがありますので、そのメモです。

github.com

Abotとは

Abotはアリゾナ州のStevenさんが開発されているC#によるクローラー用ライブラリです。

商用・個人問わず使えるオープンソース(Apache License)で、早く、カスタマイズ可能で、軽量とのことです。

また、AbotXというサイトでは、プラグインも公開されています。 (プラグイン例:複数サイトをクロールする、Jsで実行、スケジュール実行、スピード調整等)

使い方

基本的には、Config設定を設定して、クラスライブラリを読み出すだけです。

Abotの使い方

1: Nugetを使って、Abotをインストールします。

2: using句で以下の名前空間を追加します。

using Abot.Crawler;
using Abot.Poco;

3(パターン1): コードで設定する場合

public CrawlConfiguration()
{
    MaxConcurrentThreads = 10;
    UserAgentString = "Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko";
    RobotsDotTextUserAgentString = "abot";
    MaxPagesToCrawl = 1000;
    DownloadableContentTypes = "text/html";
    ConfigurationExtensions = new Dictionary<string, string>();
    MaxRobotsDotTextCrawlDelayInSeconds = 5;
    HttpRequestMaxAutoRedirects = 7;
    IsHttpRequestAutoRedirectsEnabled = true;
    MaxCrawlDepth = 100;
    HttpServicePointConnectionLimit = 200;
    HttpRequestTimeoutInSeconds = 15;
    IsSslCertificateValidationEnabled = true;
}

3(パターン2): コードではなく、App.config(Web.config)で設定する場合

  <abot>
    <crawlBehavior 
      maxConcurrentThreads="10" 
      maxPagesToCrawl="1000" 
      maxPagesToCrawlPerDomain="0" 
      maxPageSizeInBytes="0"
      userAgentString="Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko" 
      crawlTimeoutSeconds="0" 
      downloadableContentTypes="text/html, text/plain" 
      isUriRecrawlingEnabled="false" 
      isExternalPageCrawlingEnabled="false" 
      isExternalPageLinksCrawlingEnabled="false"
      httpServicePointConnectionLimit="200"  
      httpRequestTimeoutInSeconds="15" 
      httpRequestMaxAutoRedirects="7" 
      isHttpRequestAutoRedirectsEnabled="true" 
      isHttpRequestAutomaticDecompressionEnabled="false"
      isSendingCookiesEnabled="false"
      isSslCertificateValidationEnabled="false"
      isRespectUrlNamedAnchorOrHashbangEnabled="false"
      minAvailableMemoryRequiredInMb="0"
      maxMemoryUsageInMb="0"
      maxMemoryUsageCacheTimeInSeconds="0"
      maxCrawlDepth="1000"
      isForcedLinkParsingEnabled="false"
      maxRetryCount="0"
      minRetryDelayInMilliseconds="0"
      />
    <authorization
      isAlwaysLogin="false"
      loginUser=""
      loginPassword="" />     
    <politeness 
      isRespectRobotsDotTextEnabled="false"
      isRespectMetaRobotsNoFollowEnabled="false"
      isRespectHttpXRobotsTagHeaderNoFollowEnabled="false"
      isRespectAnchorRelNoFollowEnabled="false"
      isIgnoreRobotsDotTextIfRootDisallowedEnabled="false"
      robotsDotTextUserAgentString="abot"
      maxRobotsDotTextCrawlDelayInSeconds="5" 
      minCrawlDelayPerDomainMilliSeconds="0"/>
    <extensionValues>
      <add key="key1" value="value1"/>
      <add key="key2" value="value2"/>
    </extensionValues>
  </abot>  

4: クローリングの実行

//同期実行の場合
CrawlResult result = crawler.Crawl(new Uri("http://localhost:1111/")); 

//エラー制御
if (result.ErrorOccurred)
{
    Console.WriteLine("次のサイト {0} のクロールがエラーで終了しました: {1}", 
        result.RootUri.AbsoluteUri, result.ErrorException.Message);
}
else
{
    Console.WriteLine("次のサイト {0} のクロールが正常終了しました", result.RootUri.AbsoluteUri);
}

Gitから取得できるコードについて

Gitからコードをクローンすると、ライブラリに加え、サンプルソースを入手できます。

f:id:aoki1210:20160418144445p:plain

デモプロジェクトの実行

「Abot.Demo」をスタートアッププロジェクトに設定して実行すると、URLを入力するコマンドプロンプトが表示されます。 URLを入力すると、そのサイトのクロールが行われ、Binフォルダにlog4netのログが出力されます。

参照ライブラリについて

Abotが使用しているライブラリは、以下の通りです。CsQueryとNRobotsPatched は知らないライブラリでしたので、リンクを張っておきます。