> ## Documentation Index
> Fetch the complete documentation index at: https://docs.agentify.jp/llms.txt
> Use this file to discover all available pages before exploring further.

# クローラーマニュアル

> Webスクレイピングツールで利用する getHtml、xpath、regex、jsonPath などのコマンド記法を説明します。

このマニュアルでは、クローラーのフィールド抽出ルールで利用するコマンドスクリプトの書き方を説明します。各コマンドは1行ずつ記述し、最後は `get`、`all`、`rawText` などの終端コマンドで終了します。

## 1. 概要

### 1.1 構文

スクリプトは、コマンドと任意のパラメータで構成されます。

```text theme={null}
<コマンド> [=> コマンドパラメータ]
```

<CardGroup cols={3}>
  <Card title="コマンド" icon="terminal">
    必須項目です。1行に1つだけ記述します。戻り値がセレクタ型の場合は、次の行へチェーンできます。
  </Card>

  <Card title="=>" icon="code">
    パラメータを指定するための記号です。パラメータが不要なコマンドでは省略します。
  </Card>

  <Card title="コマンドパラメータ" icon="sliders">
    XPath、正規表現、jsonPath など、コマンドに渡す具体的な条件です。
  </Card>
</CardGroup>

<Warning>
  最後のコマンドは必ず終端型にしてください。セレクタ型のまま終わると、抽出結果として利用できません。
</Warning>

### 1.2 サンプル

<AccordionGroup>
  <Accordion title="サンプル 1: 記事リンクだけを抽出する" icon="link">
    すべてのリンクを取得し、`/articles/` を含むリンクだけを抽出します。

    ```shell theme={null}
    getHtml
    links
    regex=>.*/articles/.*
    all
    ```
  </Accordion>

  <Accordion title="サンプル 2: タイトルを取得する" icon="heading">
    任意の article タグ内で、`class="article-body"` の要素配下にある h1 のテキストを取得します。

    ```shell theme={null}
    getHtml
    xpath=>//article[@class='articlePage']//h1/allText()
    get
    ```
  </Accordion>

  <Accordion title="サンプル 3: 本文全体を取得する" icon="file-lines">
    任意の div タグ内で、`class="article-body"` の要素配下にある記事本文全体を取得します。

    ```shell theme={null}
    getHtml
    xpath=>//div[@class='article-body']/allText()
    get
    ```
  </Accordion>
</AccordionGroup>

## 2. コマンド一覧

| コマンド       | パラメータ      | 戻り値タイプ      | 説明                                           | 使用例                                             |
| ---------- | ---------- | ----------- | -------------------------------------------- | ----------------------------------------------- |
| `getHtml`  | なし         | セレクタ        | HTML コンテンツをセレクタとして取得します。                     | `getHtml`                                       |
| `getJson`  | なし         | セレクタ        | JSON コンテンツをセレクタとして取得します。                     | `getJson`                                       |
| `rawText`  | なし         | String      | 終端コマンド。ページ全体のテキストを取得します。                     | `rawText`                                       |
| `xpath`    | XPath 式    | セレクタ        | XPath 式に従って要素を選択します。                         | `xpath=>//div[@class='article-body']/allText()` |
| `regex`    | 正規表現       | セレクタ        | 正規表現で内容を選択します。                               | `regex=>.*/articles/.*`                         |
| `jsonPath` | jsonPath 式 | セレクタ        | jsonPath 式に従って JSON の内容を選択します。               | `jsonPath=>$.data.content`                      |
| `links`    | なし         | セレクタ        | ページ内のリンク URL を取得します。                         | `links`                                         |
| `get`      | なし         | String      | 終端コマンド。選択された要素の内容を1件取得します。                   | `get`                                           |
| `all`      | なし         | String のリスト | 終端コマンド。選択された要素の集合を取得します。主に `links` の後で利用します。 | `all`                                           |

## 3. 関連構文

### 3.1 XPath 構文

XPath は Xsoup の構文仕様に準拠しています。HTML の要素、属性、テキストを指定して抽出できます。

<CardGroup cols={2}>
  <Card title="要素を指定" icon="code">
    `//h1`、`//div` のようにタグ名で要素を指定します。
  </Card>

  <Card title="属性で絞り込む" icon="filter">
    `//div[@class='article-body']` のように属性値で対象を絞り込みます。
  </Card>

  <Card title="テキストを取得" icon="text">
    `text()` や `allText()` を使って、要素内のテキストを取得します。
  </Card>

  <Card title="階層をたどる" icon="diagram-project">
    `/` や `//` を使って、親子関係や子孫要素を指定します。
  </Card>
</CardGroup>

<Frame>
  <img src="https://mintcdn.com/trendoinc/BbOCPJdxANbL_EDU/images/crawler-script-guide/image1.jpeg?fit=max&auto=format&n=BbOCPJdxANbL_EDU&q=85&s=aa027d1659bd8fad4c1618232c185ea8" alt="XPath 1.0 のサポート構文一覧" width="1233" height="589" data-path="images/crawler-script-guide/image1.jpeg" />
</Frame>

#### よく使う XPath 例

| 目的               | XPath 例                                     |
| ---------------- | ------------------------------------------- |
| h1 のテキストを取得      | `//h1/text()`                               |
| id 指定で本文を取得      | `//div[@id='press-release-body']/allText()` |
| class 指定で本文を取得   | `//div[@class='article-body']/allText()`    |
| a タグの href 属性を取得 | `//a/@href`                                 |

<Frame>
  <img src="https://mintcdn.com/trendoinc/BbOCPJdxANbL_EDU/images/crawler-script-guide/image2.jpeg?fit=max&auto=format&n=BbOCPJdxANbL_EDU&q=85&s=f6e2df86c025f499da15e2b497622248" alt="Xsoup で利用できる関数一覧" width="1233" height="528" data-path="images/crawler-script-guide/image2.jpeg" />
</Frame>

<Frame>
  <img src="https://mintcdn.com/trendoinc/BbOCPJdxANbL_EDU/images/crawler-script-guide/image3.jpeg?fit=max&auto=format&n=BbOCPJdxANbL_EDU&q=85&s=b62bbdb509d97da432a667ebc73e84fc" alt="Xsoup の拡張構文一覧" width="1233" height="472" data-path="images/crawler-script-guide/image3.jpeg" />
</Frame>

### 3.2 regex 構文

`regex` は、取得済みのテキストやリンク一覧から、条件に一致する内容だけを抽出するために使います。

```shell theme={null}
getHtml
links
regex=>https://example\.com/articles/\d+\.html
all
```

正規表現の学習や動作確認には、以下のような練習サイトを利用できます。

<Card title="正規表現の学習サイト" icon="arrow-up-right-from-square" href="https://www.hituzi.co.jp/560regex/regex_lesson/index.html">
  基本的な正規表現を確認しながら練習できます。
</Card>

<Frame>
  <img src="https://mintcdn.com/trendoinc/BbOCPJdxANbL_EDU/images/crawler-script-guide/image4.png?fit=max&auto=format&n=BbOCPJdxANbL_EDU&q=85&s=a4efa882a4cc7429aab16bd0cc840a10" alt="正規表現の練習サイト画面" width="3174" height="1372" data-path="images/crawler-script-guide/image4.png" />
</Frame>

### 3.3 jsonPath 構文

`jsonPath` は、JSON 形式のレスポンスから特定の値を抽出するために使います。

```shell theme={null}
getJson
jsonPath=>$.data.content
get
```

jsonPath の動作確認には、以下のサイトを利用できます。

<Card title="jsonPath の確認サイト" icon="arrow-up-right-from-square" href="https://jsonpath.com/">
  JSON データと jsonPath 式を入力して、抽出結果を確認できます。
</Card>

<Frame>
  <img src="https://mintcdn.com/trendoinc/BbOCPJdxANbL_EDU/images/crawler-script-guide/image5.png?fit=max&auto=format&n=BbOCPJdxANbL_EDU&q=85&s=68f7bbf586d22e2e932e39ac0b1dcd43" alt="jsonPath の確認サイト画面" width="2980" height="884" data-path="images/crawler-script-guide/image5.png" />
</Frame>
