Node-red series (6) : The use of Node-red parsing nodes

Last time we talked about the use of sequence nodes in Node-Red, using sequence nodes we can split, merge, sort the message body. In this article, I will tell you about the Node parsing under the parser classification in Node-Red, which has 5 nodes.

  • CSV Converts cSV-formatted strings and their JavaScript object representations to and from each other.
  • HTML uses CSS selectors to extract elements from HTML documents stored in msg.payload.
  • Json converts between JSON strings and their JavaScript object representations.
  • XML converts to and from XML strings and their JavaScript object representations.
  • Yaml converts between yamL formatted strings and their JavaScript object representations.

csv

html

To use this node, we can use an HTTP request node, use the HTTP node, to get the entire content of an HTML, and then use the HTML node to parse it. When parsing, first you have to select a tag, for example, if you want to get all the P tags in an HTML, you can configure it like this, You can store all the text contents of p tags in MSG. Payload as an array. As shown in figure

The requested HTML is this, McZaiyun.top /data/1.html

It’s very simple

<! DOCTYPEhtml>
<html>

<head>
    <meta charset="utf-8">
    <meta name="author" content="http://www.softwhy.com/" />
    <title>ScrollWidth and scrollHeight properties - Ant tribe</title>
    <style type="text/css">
        * {
            margin: 0px;
            padding: 0px;
        }

        #box {
            width: 200px;
            height: 150px;
            border: 1px solid #ddd;
            margin: 0px auto;
            margin-top: 50px;
            /* overflow: scroll; * /
            padding: 0px;
        }

        #box::before {
            content: url("Data :image/ SVG + XML,%3Csvg t='1610605001506' class='icon' viewBox='0 0 1024 1024' version='1.1' xmlns='http://www.w3.org/2000/svg' p-id='9843' xmlns:xlink='http://www.w3.org/1999/xlink' width='16' Height = "16" % 3 e % 3 cdefs % 3 e % 3 cstyle type = "text/CSS" % 3 e % 3 c/style % 3 e % 3 c/defs % 3 e % 3 cpath d = 'M562.005333 512 l - 211.2-211.2 60.330667 60.330667 L682.666667 512-271.530667-271.530667-60.330667-60.330667 l z 'p - id =' 9844 ' fill='%239DA1A7'%3E%3C/path%3E%3C/svg%3E");
        }
    </style>
</head>

<body>
    <div id="box">
        <p>Paragraph 1</p>
        <p>Paragraph 2</p>
        <p>Paragraph 3</p>
        <p>Paragraph 4</p>
        <p>Paragraph 5</p>
        <p>Paragraph 6</p>
    </div>

</body>

</html>
Copy the code

Using this node, we can regularly monitor the content of a page. For example, if a person posts an article, we can send an email to someone. You can also use some external information, such as microblog, to release a microblog, to control the LED light off, on. Such actual combat case, we need to analyze the content of micro-blog, to get the content we need. Of course, you need to customize the message body, such as # on #LED or # Off #LED. Parse the corresponding content and do not use the operation.

Here is the JSON data for this stream, so you can import it and do it yourself.

[{"id": "70 d9360. 22 b68cc"."type": "tab"."label": "Flow 2"."disabled": false."info": ""
    },
    {
        "id": "c063491c.2a1318"."type": "html"."z": "70 d9360. 22 b68cc"."name": "get p"."property": "payload"."outproperty": "payload"."tag": "p"."ret": "text"."as": "single"."x": 650."y": 260."wires": [["F6760bc5. 997 d58"]]}, {"id": "F6760bc5. 997 d58"."type": "debug"."z": "70 d9360. 22 b68cc"."name": ""."active": true."tosidebar": true."console": false."tostatus": false."complete": "payload"."targetType": "msg"."statusVal": ""."statusType": "auto"."x": 900."y": 260."wires": []}, {"id": "56422139.aff37"."type": "http request"."z": "70 d9360. 22 b68cc"."name": "1.html"."method": "GET"."ret": "txt"."paytoqs": "body"."url": "http://mczaiyun.top/data/1.html"."tls": ""."persist": false."proxy": ""."authType": ""."x": 370."y": 260."wires": [["c063491c.2a1318"]]}, {"id": "27 d30977. 7 bc8e6"."type": "inject"."z": "70 d9360. 22 b68cc"."name": ""."props": [{"p": "topic"."vt": "str"}]."repeat": ""."crontab": ""."once": false."onceDelay": 0.1."topic": ""."x": 170."y": 260."wires": [["56422139.aff37"]]}]Copy the code

json

Json nodes using the same way as HTML, we request the address http://mczaiyun.top/data/wxdl.json to get a json data,

The content of this JSON is

As shown in figure

It is an article with a carved dragon in its heart

Use the DEBUG node to print the following information after processing the JSON node

xml

yaml

Yaml we can request the address of a yaml file at http://mczaiyun.top/data/pmc.yml