Node-RED HTML ノードガイド

☁️ クラウドOK 環境とは?

目次

1. 概要

HTML ノードは、HTMLドキュメントから特定の要素を抽出するノードです。CSSセレクタを使って、Webページから必要なデータを取り出す「Webスクレイピング」に使用します。

本から特定の章を探すことに例えると:HTML ノードは本の目次を使って特定の章を探すようなものです。「第3章」「著者紹介」など、目的の部分をピンポイントで取り出せます。

HTTP Request → HTML → Debug

ポイント:

動作を図で見る

html ノードは、HTML の文字列から、セレクタで指定した要素を取り出します。

入ってくる msgpayload: "<ul> <li>りんご</li> <li>みかん</li></ul>"html出ていく msgpayload: [ "りんご", "みかん"]セレクタ:li
セレクタに合う要素の中身が、配列になって出てくる

2. 設定詳細

主要プロパティ

エディターは上から順に、入力 HTML の取得元、抽出方法、出力先を指定する欄が並ぶ。

設定項目説明既定値
プロパティ解析する HTML が入っている入力プロパティ。msg のどこに HTML 文字列があるかを指すmsg.payload
抽出する要素取り出したい部分を指す CSS セレクタ(h1、.title など)。次章で詳しく解説する(空)
出力マッチした要素から何を取り出すか。下の「出力の種類」から選ぶ要素内のHTML
配列化した1つのメッセージ/要素毎の複数のメッセージ「出力」欄の下にあるセレクタ。複数の要素がマッチしたとき、結果を1つの配列にまとめて1メッセージで送るか、要素ごとに分けて複数メッセージで送るかを切り替える配列化した1つのメッセージ
対象抽出結果を書き込む出力先プロパティmsg.payload
HTMLコンテンツのプロパティ名「出力」で「要素やHTMLコンテンツの属性オブジェクト」を選んだときだけ表示される。属性に加えて要素内の HTML 本文を格納するキー名(接頭辞)_
名前エディター上のノード表示名(空)

「出力」の種類

「出力」セレクタで選べる4つの取り出し方。例は <p class="lead"><b>太字</b>テキスト</p> の p 要素を抽出した場合を示す。

選択肢説明例
要素内のHTMLマッチした要素の内側の HTML をそのまま返す<b>太字</b>テキスト
要素のテキストのみタグを除いたテキストだけを返す太字テキスト
要素の全ての属性要素が持つ属性をオブジェクトにまとめて返す{ "class": "lead" }
要素やHTMLコンテンツの属性オブジェクト属性オブジェクトに要素内の HTML 本文も加える(本文は「HTMLコンテンツのプロパティ名」で指定したキーに入る){ "class": "lead", "_": "<b>太字</b>テキスト" }

3. CSSセレクタの基本

基本セレクタ

セレクタ説明例
要素名タグ名で選択p, div, a
.クラス名class属性で選択.article, .title
#IDid属性で選択#header, #main
[属性]属性の有無で選択[href], [data-id]
[属性=値]属性値で選択[type="text"]

組み合わせセレクタ

セレクタ説明例
A BAの子孫のBdiv p(div内の全p)
A > BAの直接の子のBul > li(ulの直下のli)
A, BAまたはBh1, h2(h1とh2両方)
A.classAでクラスがclassp.intro

便利なセレクタ

/* n番目の要素 */ li:nth-child(1) /* 1番目のli */ li:nth-child(odd) /* 奇数番目のli */ li:first-child /* 最初のli */ li:last-child /* 最後のli */ /* 属性の部分一致 */ [href^="https"] /* httpsで始まる */ [href$=".pdf"] /* .pdfで終わる */ [href*="example"] /* exampleを含む */

4. 実用的な使用パターン

サンプルフローのインポート方法:

  1. 下のフローJSON(公式サンプルフロー、または応用フロー)をコピー
  2. Node-REDエディタで メニュー → 読み込み を選択
  3. JSONをペーストして「読み込み」をクリック

公式サンプルフロー

最初のサンプルフローは、Node-RED本体に同梱されている公式サンプル4つ(CSS セレクタで要素を配列として取り出す例、メッセージ列として取り出す例、セレクタを msg で渡す例、取り出した列を join でつなげる例)を1つのタブにまとめ、名前の無かった inject ノードと debug ノードに日本語の名前を付けたものです。エディタの メニュー → 読み込み → サンプル → node-red → parser → html からも、元のフローを開けます。

📥 公式サンプルフローJSON(クリックで展開)
[ { "id": "b7b0d9c4bd4d50c9", "type": "tab", "label": "html", "disabled": false, "info": "", "env": [] }, { "id": "8c5224a6.201b88", "type": "inject", "z": "b7b0d9c4bd4d50c9", "name": "例1: 実行", "props": [ { "p": "payload" }, { "p": "topic", "vt": "str" } ], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "", "payloadType": "date", "x": 220, "y": 180, "wires": [ [ "d6c67e51.0d709" ] ] }, { "id": "d6c67e51.0d709", "type": "template", "z": "b7b0d9c4bd4d50c9", "name": "HTML text", "field": "payload", "fieldType": "msg", "format": "handlebars", "syntax": "plain", "template": "<html>\n <head>\n <title>List of Fruits</title>\n </head>\n <body>\n <ul>\n <li class=\"Item\">Apple</li>\n <li class=\"Item\">Orange</li>\n <li class=\"Item\">Banana</li>\n </ul>\n </body>\n</html>\n", "output": "str", "x": 390, "y": 180, "wires": [ [ "599a1155.61a5c" ] ] }, { "id": "b0d5cd89.338df", "type": "comment", "z": "b7b0d9c4bd4d50c9", "name": "Extract array of HTML element by CSS selector", "info": "HTML node can be used to extract elements in HTML document as an array using CSS selector.", "x": 280, "y": 120, "wires": [] }, { "id": "599a1155.61a5c", "type": "html", "z": "b7b0d9c4bd4d50c9", "name": "", "property": "payload", "outproperty": "payload", "tag": ".Item", "ret": "html", "as": "single", "x": 550, "y": 180, "wires": [ [ "942b23d1.cce09" ] ] }, { "id": "942b23d1.cce09", "type": "debug", "z": "b7b0d9c4bd4d50c9", "name": "例1: 結果", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "false", "statusVal": "", "statusType": "auto", "x": 710, "y": 180, "wires": [] }, { "id": "a44973e8.6319b", "type": "inject", "z": "b7b0d9c4bd4d50c9", "name": "例2: 実行", "props": [ { "p": "payload" }, { "p": "topic", "vt": "str" } ], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "", "payloadType": "date", "x": 220, "y": 360, "wires": [ [ "de1b012e.96ec3" ] ] }, { "id": "de1b012e.96ec3", "type": "template", "z": "b7b0d9c4bd4d50c9", "name": "HTML text", "field": "payload", "fieldType": "msg", "format": "handlebars", "syntax": "plain", "template": "<html>\n <head>\n <title>List of Fruits</title>\n </head>\n <body>\n <ul>\n <li class=\"Item\">Apple</li>\n <li class=\"Item\">Orange</li>\n <li class=\"Item\">Banana</li>\n </ul>\n </body>\n</html>\n", "output": "str", "x": 390, "y": 360, "wires": [ [ "cee70712.6f3538" ] ] }, { "id": "99e32bc7.c8e508", "type": "comment", "z": "b7b0d9c4bd4d50c9", "name": "Extract sequence of HTML element by CSS selector", "info": "HTML node can be used to extract elements in HTML document as a messege sequence using CSS selector.", "x": 290, "y": 300, "wires": [] }, { "id": "cee70712.6f3538", "type": "html", "z": "b7b0d9c4bd4d50c9", "name": "", "property": "payload", "outproperty": "payload", "tag": ".Item", "ret": "html", "as": "multi", "x": 550, "y": 360, "wires": [ [ "17f25482.d4b56b" ] ] }, { "id": "17f25482.d4b56b", "type": "debug", "z": "b7b0d9c4bd4d50c9", "name": "例2: 結果", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "false", "statusVal": "", "statusType": "auto", "x": 710, "y": 360, "wires": [] }, { "id": "653ce9aa.b6a1c8", "type": "inject", "z": "b7b0d9c4bd4d50c9", "name": "例3: 実行", "props": [ { "p": "payload" }, { "p": "topic", "vt": "str" } ], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "", "payloadType": "date", "x": 220, "y": 560, "wires": [ [ "52a16f7f.447d8" ] ] }, { "id": "52a16f7f.447d8", "type": "template", "z": "b7b0d9c4bd4d50c9", "name": "HTML text", "field": "payload", "fieldType": "msg", "format": "handlebars", "syntax": "plain", "template": "<html>\n <head>\n <title>List of Fruits</title>\n </head>\n <body>\n <ul>\n <li class=\"Item\">Apple</li>\n <li class=\"Item\">Orange</li>\n <li class=\"Item\">Banana</li>\n </ul>\n </body>\n</html>\n", "output": "str", "x": 390, "y": 560, "wires": [ [ "a52319c3.89b008" ] ] }, { "id": "8bc35379.31d99", "type": "comment", "z": "b7b0d9c4bd4d50c9", "name": "Extract array of HTML element by CSS selector specified in message", "info": "CSS selector for HTML node can be specified by `select` property of input message.", "x": 350, "y": 500, "wires": [] }, { "id": "9c49de8a.bad25", "type": "html", "z": "b7b0d9c4bd4d50c9", "name": "", "property": "payload", "outproperty": "payload", "tag": "", "ret": "html", "as": "single", "x": 730, "y": 560, "wires": [ [ "d4f4b987.278a68" ] ] }, { "id": "d4f4b987.278a68", "type": "debug", "z": "b7b0d9c4bd4d50c9", "name": "例3: 結果", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "false", "statusVal": "", "statusType": "auto", "x": 890, "y": 560, "wires": [] }, { "id": "a52319c3.89b008", "type": "change", "z": "b7b0d9c4bd4d50c9", "name": "", "rules": [ { "t": "set", "p": "select", "pt": "msg", "to": ".Item", "tot": "str" } ], "action": "", "property": "", "from": "", "to": "", "reg": false, "x": 560, "y": 560, "wires": [ [ "9c49de8a.bad25" ] ] }, { "id": "66cff4ee.f2761c", "type": "inject", "z": "b7b0d9c4bd4d50c9", "name": "例4: 実行", "props": [ { "p": "payload" }, { "p": "topic", "vt": "str" } ], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "", "payloadType": "date", "x": 220, "y": 760, "wires": [ [ "2baaf6bf.0a02ca" ] ] }, { "id": "2baaf6bf.0a02ca", "type": "template", "z": "b7b0d9c4bd4d50c9", "name": "HTML text", "field": "payload", "fieldType": "msg", "format": "handlebars", "syntax": "plain", "template": "<html>\n <head>\n <title>List of Fruits</title>\n </head>\n <body>\n <ul>\n <li class=\"Item\">Apple</li>\n <li class=\"Item\">Orange</li>\n <li class=\"Item\">Banana</li>\n </ul>\n </body>\n</html>\n", "output": "str", "x": 390, "y": 760, "wires": [ [ "bbb22e6b.0fa25" ] ] }, { "id": "a57d35d0.8aa538", "type": "comment", "z": "b7b0d9c4bd4d50c9", "name": "Join extracted sequence of HTML element using join node", "info": "Message sequence extracted by HTML node can be combined using join node.", "x": 310, "y": 700, "wires": [] }, { "id": "bbb22e6b.0fa25", "type": "html", "z": "b7b0d9c4bd4d50c9", "name": "", "property": "payload", "outproperty": "payload", "tag": ".Item", "ret": "html", "as": "multi", "x": 550, "y": 760, "wires": [ [ "bd01ca4.966ad38" ] ] }, { "id": "4d2616a8.84de88", "type": "debug", "z": "b7b0d9c4bd4d50c9", "name": "例4: 結果", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "false", "statusVal": "", "statusType": "auto", "x": 850, "y": 760, "wires": [] }, { "id": "bd01ca4.966ad38", "type": "join", "z": "b7b0d9c4bd4d50c9", "name": "", "mode": "custom", "build": "string", "property": "payload", "propertyType": "msg", "key": "topic", "joiner": ",", "joinerType": "str", "accumulate": false, "timeout": "", "count": "", "reduceRight": false, "reduceExp": "", "reduceInit": "", "reduceInitType": "", "reduceFixup": "", "x": 690, "y": 760, "wires": [ [ "4d2616a8.84de88" ] ] } ]

フローの読み解き

フローには、公式サンプルの4つの例が、上から順に並んでいます。それぞれの先頭に、英語の comment ノードがあります。どの例も、「例N: 実行」(inject)のボタンで動かし、「例N: 結果」(debug)に結果が出ます。4つの例は、同じ HTML(「HTML text」という名前の template ノードに書いてある、果物3つの箇条書き)から、class="Item" の付いた li 要素を取り出します。違うのは、取り出した結果の出し方です。

例 comment ノードの見出し(意味) ノードの並びと、html ノードの設定 「例N: 結果」に出るもの
例1 Extract array of HTML element by CSS selector(CSS セレクタで、HTML の要素を配列として取り出す) 「例1: 実行」→「HTML text」(template)→ html →「例1: 結果」。「抽出する要素」は .Item、「出力」は「要素内のHTML」と「配列化した1つのメッセージ」 ["Apple","Orange","Banana"](配列が1つ)
例2 Extract sequence of HTML element by CSS selector(メッセージ列として取り出す) 並びは例1と同じ。「出力」の2つ目の欄が「要素毎の複数のメッセージ」 Apple、Orange、Banana(別々の msg が3つ)
例3 Extract array of HTML element by CSS selector specified in message(セレクタを msg で渡して、配列として取り出す) 「例3: 実行」→「HTML text」→ change → html →「例3: 結果」。change が msg.select に .Item を入れる。html ノードの「抽出する要素」は空にしてある ["Apple","Orange","Banana"](例1と同じ)
例4 Join extracted sequence of HTML element using join node(取り出した列を、join ノードでつなげる) 「例4: 実行」→「HTML text」→ html → join →「例4: 結果」。html ノードは例2と同じ設定。join は、「動作」が「手動」で、カンマ区切りの文字列にまとめる設定 Apple,Orange,Banana(文字列が1つ)

応用(サイト独自): 見出し・リンク・クラス・表からの取り出し

ここから先は公式サンプルではなく、本サイトが用意した応用教材です。タグ名で取り出す部分、クラスで絞り込む部分、表のセルを取り出す部分があり、下のパターン1〜3の実例になっています。

📥 応用フローJSON(クリックで展開)
[ { "id": "html_sample_tab", "type": "tab", "label": "HTML サンプル", "disabled": false, "info": "" }, { "id": "html_comment1", "type": "comment", "z": "html_sample_tab", "name": "━━━ 基本: HTMLからテキスト抽出 ━━━", "info": "", "x": 190, "y": 40, "wires": [] }, { "id": "html_template1", "type": "template", "z": "html_sample_tab", "name": "サンプルHTML", "field": "payload", "fieldType": "msg", "format": "html", "syntax": "plain", "template": "<html>\n<body>\n <h1>メインタイトル</h1>\n <div class=\"content\">\n <p class=\"intro\">これは紹介文です。</p>\n <p>本文の段落1です。</p>\n <p>本文の段落2です。</p>\n </div>\n <ul class=\"links\">\n <li><a href=\"https://example.com/page1\">リンク1</a></li>\n <li><a href=\"https://example.com/page2\">リンク2</a></li>\n <li><a href=\"https://example.com/page3\">リンク3</a></li>\n </ul>\n</body>\n</html>", "output": "str", "x": 160, "y": 100, "wires": [["html_extract_h1", "html_extract_p", "html_extract_links"]] }, { "id": "html_inject1", "type": "inject", "z": "html_sample_tab", "name": "実行", "props": [], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "x": 130, "y": 60, "wires": [["html_template1"]] }, { "id": "html_extract_h1", "type": "html", "z": "html_sample_tab", "name": "h1抽出", "property": "payload", "outproperty": "payload", "tag": "h1", "ret": "text", "as": "single", "chr": "_", "x": 350, "y": 60, "wires": [["html_debug1"]] }, { "id": "html_debug1", "type": "debug", "z": "html_sample_tab", "name": "h1: メインタイトル", "active": true, "tosidebar": true, "console": false, "tostatus": true, "complete": "payload", "targetType": "msg", "statusVal": "payload", "statusType": "auto", "x": 550, "y": 60, "wires": [] }, { "id": "html_extract_p", "type": "html", "z": "html_sample_tab", "name": "全p抽出", "property": "payload", "outproperty": "payload", "tag": "p", "ret": "text", "as": "multi", "chr": "_", "x": 350, "y": 100, "wires": [["html_debug2"]] }, { "id": "html_debug2", "type": "debug", "z": "html_sample_tab", "name": "p: 1件ずつ出力", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "payload", "targetType": "msg", "x": 540, "y": 100, "wires": [] }, { "id": "html_extract_links", "type": "html", "z": "html_sample_tab", "name": "リンクURL抽出", "property": "payload", "outproperty": "payload", "tag": "a", "ret": "attr", "as": "multi", "chr": "_", "x": 360, "y": 140, "wires": [["html_debug3"]] }, { "id": "html_debug3", "type": "debug", "z": "html_sample_tab", "name": "href属性", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "payload", "targetType": "msg", "x": 540, "y": 140, "wires": [] }, { "id": "html_comment2", "type": "comment", "z": "html_sample_tab", "name": "━━━ クラスやIDで絞り込み ━━━", "info": "", "x": 180, "y": 200, "wires": [] }, { "id": "html_inject2", "type": "inject", "z": "html_sample_tab", "name": "実行", "props": [], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "x": 130, "y": 260, "wires": [["html_template2"]] }, { "id": "html_template2", "type": "template", "z": "html_sample_tab", "name": "クラス付きHTML", "field": "payload", "fieldType": "msg", "format": "html", "syntax": "plain", "template": "<div id=\"news\">\n <article class=\"news-item featured\">\n <h2 class=\"title\">重要ニュース</h2>\n <p class=\"summary\">重要な出来事の概要</p>\n </article>\n <article class=\"news-item\">\n <h2 class=\"title\">通常ニュース1</h2>\n <p class=\"summary\">概要1</p>\n </article>\n <article class=\"news-item\">\n <h2 class=\"title\">通常ニュース2</h2>\n <p class=\"summary\">概要2</p>\n </article>\n</div>", "output": "str", "x": 170, "y": 300, "wires": [["html_extract_class"]] }, { "id": "html_extract_class", "type": "html", "z": "html_sample_tab", "name": ".news-item .title", "property": "payload", "outproperty": "payload", "tag": ".news-item .title", "ret": "text", "as": "multi", "chr": "_", "x": 370, "y": 300, "wires": [["html_debug4"]] }, { "id": "html_debug4", "type": "debug", "z": "html_sample_tab", "name": "ニュースタイトル", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "payload", "targetType": "msg", "x": 570, "y": 300, "wires": [] }, { "id": "html_comment3", "type": "comment", "z": "html_sample_tab", "name": "━━━ テーブルデータ抽出 ━━━", "info": "", "x": 180, "y": 360, "wires": [] }, { "id": "html_inject3", "type": "inject", "z": "html_sample_tab", "name": "実行", "props": [], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "x": 130, "y": 420, "wires": [["html_template3"]] }, { "id": "html_template3", "type": "template", "z": "html_sample_tab", "name": "テーブルHTML", "field": "payload", "fieldType": "msg", "format": "html", "syntax": "plain", "template": "<table>\n <tr><th>名前</th><th>年齢</th><th>職業</th></tr>\n <tr><td>田中</td><td>30</td><td>エンジニア</td></tr>\n <tr><td>鈴木</td><td>25</td><td>デザイナー</td></tr>\n <tr><td>佐藤</td><td>35</td><td>マネージャー</td></tr>\n</table>", "output": "str", "x": 160, "y": 460, "wires": [["html_extract_table"]] }, { "id": "html_extract_table", "type": "html", "z": "html_sample_tab", "name": "td抽出", "property": "payload", "outproperty": "payload", "tag": "table tr td", "ret": "text", "as": "multi", "chr": "_", "x": 350, "y": 460, "wires": [["html_debug5"]] }, { "id": "html_debug5", "type": "debug", "z": "html_sample_tab", "name": "テーブルデータ", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "payload", "targetType": "msg", "x": 540, "y": 460, "wires": [] } ]

フローの読み解き

応用フローは、3つの部分に分かれています。各部分の先頭にある comment ノードの名前が、下の表の「部分」に当たります。3つの inject はどれも「実行」という同じ名前なので、comment ノードのすぐ下にあるものを押してください。

部分 ノードの並び html ノードの設定と、debug に出るもの パターン
基本: HTMLからテキスト抽出 「実行」(inject)→「サンプルHTML」(template)から、「h1抽出」「全p抽出」「リンクURL抽出」の3つの html ノードへ同じ msg を送り、それぞれの debug に出す 「h1抽出」は h1・「要素のテキストのみ」・「配列化した1つのメッセージ」で、「h1: メインタイトル」に ["メインタイトル"] が出る(1件でも配列になる)。「全p抽出」は p・「要素のテキストのみ」・「要素毎の複数のメッセージ」で、「p: 1件ずつ出力」に3つの msg(段落の文)が出る。「リンクURL抽出」は a・「要素の全ての属性」・「要素毎の複数のメッセージ」で、「href属性」に {"href":"https://example.com/page1"} などのオブジェクトが3つ出る パターン1
クラスやIDで絞り込み 「実行」(inject)→「クラス付きHTML」(template)→「.news-item .title」(html)→「ニュースタイトル」(debug) 「抽出する要素」は .news-item .title(クラス news-item の要素の中にある、クラス title の要素)。「ニュースタイトル」に、重要ニュース、通常ニュース1、通常ニュース2 の3つの msg が出る。同じ記事の中の summary の文は出ない パターン2
テーブルデータ抽出 「実行」(inject)→「テーブルHTML」(template)→「td抽出」(html)→「テーブルデータ」(debug) 「抽出する要素」は table tr td。「テーブルデータ」に、田中、30、エンジニア、鈴木…と、セル1つにつき1つの msg が、合わせて9つ出る。見出しの行(th)は含まれない。行ごとにはまとまらないので、どのセルがどの行かは、届いた順番(3つで1行)で見分ける パターン3

パターン1: 基本テキスト抽出(h1、p、リンク)

応用フローの「基本: HTMLからテキスト抽出」を参照してください。

サンプルHTML → h1抽出 → メインタイトル

用途: タグ名を指定した見出し・本文・リンクの抽出

ポイント:

パターン2: クラスやIDで絞り込み

応用フローの「クラスやIDで絞り込み」を参照してください。

クラス付きHTML → .news-item .title → ニュースタイトル

用途: クラス名やID名による対象要素の絞り込み

ポイント:

パターン3: テーブルデータ抽出

応用フローの「テーブルデータ抽出」を参照してください。

テーブルHTML → table tr td → テーブルデータ

用途: テーブルの各セルからのデータ抽出

ポイント:

5. 演習問題

演習1: 見出し抽出 初級

課題: HTMLから全ての h2 タグのテキストを抽出してください。

💼 使いどころ: HTMLの中から見出しだけを一覧として取り出したい場面で使います。

成功の条件:

💡 ヒント

「抽出する要素」に h2、「出力」で「要素のテキストのみ」を選びます。

✅ 解答例フロー
[ {"id": "ex1_tab", "type": "tab", "label": "演習1"}, {"id": "ex1_inject", "type": "inject", "z": "ex1_tab", "name": "実行", "props": [], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "x": 130, "y": 80, "wires": [["ex1_template"]]}, {"id": "ex1_template", "type": "template", "z": "ex1_tab", "name": "HTML", "field": "payload", "fieldType": "msg", "format": "html", "syntax": "plain", "template": "<h1>タイトル</h1>\n<h2>セクション1</h2>\n<p>本文1</p>\n<h2>セクション2</h2>\n<p>本文2</p>\n<h2>セクション3</h2>\n<p>本文3</p>", "output": "str", "x": 270, "y": 80, "wires": [["ex1_html"]]}, {"id": "ex1_html", "type": "html", "z": "ex1_tab", "name": "h2抽出", "property": "payload", "outproperty": "payload", "tag": "h2", "ret": "text", "as": "multi", "chr": "_", "x": 410, "y": 80, "wires": [["ex1_debug"]]}, {"id": "ex1_debug", "type": "debug", "z": "ex1_tab", "name": "結果", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "payload", "targetType": "msg", "x": 550, "y": 80, "wires": []} ]

演習2: リンクURL抽出 初級

課題: HTML内の全ての a タグから href 属性を抽出してください。

💼 使いどころ: ページ内のリンクからURLだけをまとめて取り出したい場面で使います。

成功の条件:

💡 ヒント

「抽出する要素」に a、「出力」で「要素の全ての属性」と「要素毎の複数のメッセージ」を選びます。属性は、{"href":"…"} の形のオブジェクトで出ます(属性の名前を指定して、値だけを取り出す選択肢はありません)。

✅ 解答例フロー
[ {"id": "ex2_tab", "type": "tab", "label": "演習2"}, {"id": "ex2_inject", "type": "inject", "z": "ex2_tab", "name": "実行", "props": [], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "x": 130, "y": 80, "wires": [["ex2_template"]]}, {"id": "ex2_template", "type": "template", "z": "ex2_tab", "name": "リンクHTML", "field": "payload", "fieldType": "msg", "format": "html", "syntax": "plain", "template": "<nav>\n <a href=\"/home\">ホーム</a>\n <a href=\"/about\">会社概要</a>\n <a href=\"/contact\">お問い合わせ</a>\n <a href=\"https://external.com\">外部リンク</a>\n</nav>", "output": "str", "x": 280, "y": 80, "wires": [["ex2_html"]]}, {"id": "ex2_html", "type": "html", "z": "ex2_tab", "name": "href抽出", "property": "payload", "outproperty": "payload", "tag": "a", "ret": "attr", "as": "multi", "chr": "_", "x": 430, "y": 80, "wires": [["ex2_debug"]]}, {"id": "ex2_debug", "type": "debug", "z": "ex2_tab", "name": "URL一覧", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "payload", "targetType": "msg", "x": 570, "y": 80, "wires": []} ]

演習3: クラスで絞り込み 中級

課題: class="highlight" を持つ要素のテキストのみを抽出してください。

💼 使いどころ: リストの中から重要な項目だけを選び出したい場面で使います。

成功の条件:

💡 ヒント

「抽出する要素」に .highlight を指定します。

✅ 解答例フロー
[ {"id": "ex3_tab", "type": "tab", "label": "演習3"}, {"id": "ex3_inject", "type": "inject", "z": "ex3_tab", "name": "実行", "props": [], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "x": 130, "y": 80, "wires": [["ex3_template"]]}, {"id": "ex3_template", "type": "template", "z": "ex3_tab", "name": "HTML", "field": "payload", "fieldType": "msg", "format": "html", "syntax": "plain", "template": "<ul>\n <li>通常項目1</li>\n <li class=\"highlight\">重要項目1</li>\n <li>通常項目2</li>\n <li class=\"highlight\">重要項目2</li>\n <li>通常項目3</li>\n</ul>", "output": "str", "x": 270, "y": 80, "wires": [["ex3_html"]]}, {"id": "ex3_html", "type": "html", "z": "ex3_tab", "name": ".highlight", "property": "payload", "outproperty": "payload", "tag": ".highlight", "ret": "text", "as": "multi", "chr": "_", "x": 420, "y": 80, "wires": [["ex3_debug"]]}, {"id": "ex3_debug", "type": "debug", "z": "ex3_tab", "name": "重要項目のみ", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "payload", "targetType": "msg", "x": 580, "y": 80, "wires": []} ]

演習4: 複合セレクタ 上級

課題: id="main" の div 内にある、class="item" を持つ要素のテキストを抽出してください。

💼 使いどころ: ページ内の特定の領域に絞って項目を取り出したい場面で使います。

成功の条件:

💡 ヒント

複合セレクタ: #main .item

✅ 解答例フロー
[ {"id": "ex4_tab", "type": "tab", "label": "演習4"}, {"id": "ex4_inject", "type": "inject", "z": "ex4_tab", "name": "実行", "props": [], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "x": 130, "y": 80, "wires": [["ex4_template"]]}, {"id": "ex4_template", "type": "template", "z": "ex4_tab", "name": "HTML", "field": "payload", "fieldType": "msg", "format": "html", "syntax": "plain", "template": "<div id=\"sidebar\">\n <span class=\"item\">サイドバー項目</span>\n</div>\n<div id=\"main\">\n <span class=\"item\">メイン項目1</span>\n <span class=\"item\">メイン項目2</span>\n <span>その他</span>\n</div>", "output": "str", "x": 270, "y": 80, "wires": [["ex4_html"]]}, {"id": "ex4_html", "type": "html", "z": "ex4_tab", "name": "#main .item", "property": "payload", "outproperty": "payload", "tag": "#main .item", "ret": "text", "as": "multi", "chr": "_", "x": 420, "y": 80, "wires": [["ex4_debug"]]}, {"id": "ex4_debug", "type": "debug", "z": "ex4_tab", "name": "メイン内のみ", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "payload", "targetType": "msg", "x": 580, "y": 80, "wires": []} ]

6. まとめ

重要ポイント:

注意事項:

7. 実務活用例

ケース1: ニュースサイトからの情報収集

ニュースサイトから記事タイトルとリンクを定期的に取得してデータベースに保存。

ケース2: 価格監視

ECサイトから商品価格を抽出し、価格変動を監視して通知。

ケース3: データ変換

HTMLレポートから表データを抽出してJSON形式に変換。

ケース4: コンテンツ集約

複数サイトから特定のコンテンツを抽出してダッシュボードに表示。

📚 Node-RED 公式ドキュメント

🏠