处理不同数据类型#
¥Processing different data types
本章将介绍如何使用 n8n 核心节点 处理不同类型的数据。
¥In this chapter, you will learn how to process different types of data using n8n core nodes.
HTML 和 XML 数据#
¥HTML and XML data
你很可能熟悉 HTML 和 XML。
¥You're most likely familiar with HTML and XML.
HTML vs. XML
HTML 是一种标记语言,用于描述网页的结构和语义。XML 看起来与 HTML 类似,但标签名称不同,因为它们描述了所包含的数据类型。
¥HTML is a markup language used to describe the structure and semantics of a web page. XML looks similar to HTML, but the tag names are different, as they describe the kind of data they hold.
如果需要在 n8n 工作流中处理 HTML 或 XML 数据,请使用 HTML 节点 或 XML 节点。
¥If you need to process HTML or XML data in your n8n workflows, use the HTML node or the XML node.
使用 HTML 节点通过引用 CSS 选择器提取网页的 HTML 内容。如果你想从网站收集结构化信息(网页抓取),这将非常有用。
¥Use the HTML node to extract HTML content of a webpage by referencing CSS selectors. This is useful if you want to collect structured information from a website (web-scraping).
HTML 练习#
¥HTML Exercise
让我们获取 n8n 最新博客文章的标题:
¥Let's get the title of the latest n8n blog post:
- 使用 HTTP 请求节点向 URL
https://blog.n8n.io/发送 GET 请求(此端点无需身份验证)。
¥Use the HTTP Request node to make a GET request to the URL https://blog.n8n.io/ (this endpoint requires no authentication).
2. 连接一个 HTML 节点,并将其配置为提取页面上第一篇博客文章的标题。
¥Connect an HTML node and configure it to extract the title of the first blog post on the page.
-
提示:如果你不熟悉 CSS 选择器或 HTML,CSS 选择器
.post .item-title a应该能帮到你!¥Hint: If you're not familiar with CSS selectors or reading HTML, the CSS selector
.post .item-title ashould help!
显示解决方案
¥??? note "Show me the solution"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | |
使用 XML 节点将 XML 转换为 JSON,并将 JSON 转换为 XML。如果你需要处理使用 XML 或 JSON 的不同 Web 服务,并在它们之间以这两种格式获取和提交数据,则此操作非常有用。
¥Use the XML node to convert XML to JSON and JSON to XML. This operation is useful if you work with different web services that use either XML or JSON and need to get and submit data between them in the two formats.
XML 练习#
¥XML Exercise
在 第一章最终练习 中,你使用 HTTP 请求节点向 PokéAPI 发出请求。在本练习中,我们将再次使用相同的 API,但这次我们将输出转换为 XML 格式:
¥In the final exercise of Chapter 1, you used an HTTP Request node to make a request to the PokéAPI. In this exercise, we'll return to that same API but we'll convert the output to XML:
- 添加一个 HTTP 请求节点,向
https://pokeapi.co/api/v2/pokemon中的 PokéAPI 发出相同的请求。
¥Add an HTTP Request node that makes the same request to the PokéAPI at https://pokeapi.co/api/v2/pokemon.
2. 使用 XML 节点将 JSON 输出转换为 XML。
¥Use the XML node to convert the JSON output to XML.
显示解决方案
¥??? note "Show me the solution"
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
日期、时间和间隔数据#
¥Date, time, and interval data
日期和时间数据类型包括 DATE、TIME、DATETIME、TIMESTAMP 和 YEAR。日期和时间可以采用不同的格式,例如:
¥Date and time data types include DATE, TIME, DATETIME, TIMESTAMP, and YEAR. The dates and times can be passed in different formats, for example:
DATE:2022 年 3 月 29 日,29-03-2022,2022/03/29
¥DATE: March 29 2022, 29-03-2022, 2022/03/29
-
TIME:08:30:00, 8:30, 20:30 -
DATETIME:2022/03/29 08:30:00 -
TIMESTAMP:1616108400(Unix 时间戳),1616108400000(Unix 毫秒时间戳)
¥TIMESTAMP: 1616108400 (Unix timestamp), 1616108400000 (Unix ms timestamp)
YEAR:2022, 22
你可以通过以下几种方式处理日期和时间:
¥There are a few ways you can work with dates and times:
- 使用 日期和时间节点 节点将日期和时间数据转换为不同的格式并计算日期。
¥Use the Date & Time node to convert date and time data to different formats and calculate dates.
- 使用 调度触发器节点 安排工作流在特定时间、间隔或持续时间运行。
¥Use Schedule Trigger node to schedule workflows to run at a specific time, interval, or duration.
有时,你可能需要暂停工作流的执行。如果你知道某个服务无法立即处理数据或返回所有结果的速度很慢,则可能需要这样做。在这些情况下,你不希望 n8n 将不完整的数据传递给下一个节点。
¥Sometimes, you might need to pause the workflow execution. This might be necessary if you know that a service doesn't process the data instantly or it's slow to return all the results. In these cases, you don't want n8n to pass incomplete data to the next node.
如果你遇到类似情况,请在要延迟的节点后使用 等待节点。等待节点会暂停工作流的执行,并在以下情况下恢复执行:
¥If you run into situations like this, use the Wait node after the node that you want to delay. The Wait node pauses the workflow execution and will resume execution:
- 在特定时间。
¥At a specific time.
- 在指定的时间间隔后。
¥After a specified time interval.
- Webhook 调用时
¥On a webhook call.
日期练习#
¥Date Exercise
构建一个工作流,将之前使用的“客户数据存储”节点中的输入日期加上五天。然后,如果计算出的日期晚于 1959 年,则工作流会在将计算出的日期作为值 setting 之前等待 1 分钟。工作流应每 30 分钟触发一次。
¥Build a workflow that adds five days to an input date from the Customer Datastore node that you used before. Then, if the calculated date occurred after 1959, the workflow waits 1 minute before setting the calculated date as a value. The workflow should be triggered every 30 minutes.
开始:
¥To begin:
- 添加“客户数据存储(n8n 培训)”节点,并选择“获取所有人员”操作。全部返回。
¥Add the Customer Datastore (n8n training) node with the Get All People action selected. Return All. 2. 添加“日期和时间”节点,将数据存储中创建的日期向上取整到月底。将此输出到字段 new-date。包含所有输入字段。
¥Add the Date & Time node to Round Up the created Date from the datastore to End of Month. Output this to field new-date. Include all input fields.
3. 添加 If 节点,检查新的四舍五入日期是否在 1960-01-01 00:00:00 之后。
¥Add the If node to check if that new rounded date is after 1960-01-01 00:00:00.
4. 将 Wait 节点添加到该节点的 True 输出,并将其设置为等待一分钟。
¥Add the Wait node to the True output of that node and set it to wait for one minute. 5. 添加“编辑字段(设置)”节点,将名为 outputValue 的新字段设置为包含 new-date 的字符串。包含所有输入字段。
¥Add the Edit Fields (Set) node to set a new field called outputValue to a String containing new-date. Include all input fields. 6. 在工作流开头添加计划触发节点,使其每 30 分钟触发一次。(你可以保留 手动触发节点 节点用于测试!)
¥Add the Schedule Trigger node at the beginning of the workflow to trigger it every 30 minutes. (You can keep the Manual Trigger node for testing!)
显示解决方案
¥??? note "Show me the solution"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | |
二进制数据#
¥Binary data
到目前为止,你主要处理的是文本数据。但如果你想要处理非文本数据,例如图片或 PDF 文件,该怎么办?这些类型的文件以二进制数字系统表示,因此它们被视为二进制数据。这种形式的二进制数据无法提供有效信息,因此你需要将其转换为可读格式。
¥Up to now, you have mainly worked with text data. But what if you want to process data that's not text, like images or PDF files? These types of files are represented in the binary numeral system, so they're considered binary data. In this form, binary data doesn't offer you useful information, so you'll need to convert it into a readable form.
在 n8n 中,你可以使用以下节点处理二进制数据:
¥In n8n, you can process binary data with the following nodes:
- HTTP 请求 用于从/向 Web 资源和 API 请求和发送文件。
¥HTTP Request to request and send files from/to web resources and APIs.
- 从磁盘读取/写入文件 用于从/向运行 n8n 的机器读取和写入文件。
¥Read/Write Files from Disk to read and write files from/to the machine where n8n is running.
- 转换为文件 用于获取输入数据并将其输出为文件。
¥Convert to File to take input data and output it as a file.
- 从文件提取 用于从二进制格式获取数据并将其转换为 JSON。
¥Extract From File to get data from a binary format and convert it to JSON.
Reading and writing files is only available on self-hosted n8n
n8n Cloud 不支持磁盘文件读写。你将拥有对安装 n8n 的计算机的读写权限。如果你在 Docker 中运行 n8n,则你的命令将在 n8n 容器中运行,而不是在 Docker 主机中运行。“从磁盘读取/写入文件”节点会查找相对于 n8n 安装路径的文件。n8n 建议使用绝对文件路径以避免任何错误。
¥Reading and writing files to disk isn't available on n8n Cloud. You'll read and write to the machine where you installed n8n. If you run n8n in Docker, your command runs in the n8n container and not the Docker host. The Read/Write Files From Disk node looks for files relative to the n8n install path. n8n recommends using absolute file paths to prevent any errors.
要读取或写入二进制文件,你需要在节点的 File(s) Selector 参数(用于读取操作)或节点的 File Path and Name 参数(用于写入操作)中写入文件路径(位置)。
¥To read or write a binary file, you need to write the path (location) of the file in the node's File(s) Selector parameter (for the Read operation) or in the node's File Path and Name parameter (for the Write operation).
Naming the right path
文件路径会根据你运行 n8n 的方式略有不同:
¥The file path looks slightly different depending on how you are running n8n:
-
npm:
~/my_file.json -
n8n 云/Docker:
/tmp/my_file.json
¥n8n cloud / Docker: /tmp/my_file.json
二进制练习 1#
¥Binary Exercise 1
第一个二进制练习,我们将把 PDF 文件转换为 JSON:
¥For our first binary exercise, let's convert a PDF file to JSON:
- 发送 HTTP 请求以获取此 PDF 文件:
https://media.kaspersky.com/pdf/Kaspersky_Lab_Whitepaper_Anti_blocker.pdf.
¥Make an HTTP request to get this PDF file: https://media.kaspersky.com/pdf/Kaspersky_Lab_Whitepaper_Anti_blocker.pdf.
2. 使用 Extract From File 节点将文件从二进制转换为 JSON。
¥Use the Extract From File node to convert the file from binary to JSON.
显示解决方案
¥??? note "Show me the solution"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | |
二进制练习 2#
¥Binary Exercise 2
第二个二进制练习,我们将把一些 JSON 数据转换为二进制:
¥For our second binary exercise, let's convert some JSON data to binary:
- 向 Poetry DB API
https://poetrydb.org/random/1发送 HTTP 请求。
¥Make an HTTP request to the Poetry DB API https://poetrydb.org/random/1.
2. 使用“转换为文件”节点将返回的 JSON 数据转换为二进制数据
¥Convert the returned data from JSON to binary using the Convert to File node. 3. 使用“从磁盘读取/写入文件”节点,将新的二进制文件数据写入运行 n8n 的计算机。
¥Write the new binary file data to the machine where n8n is running using the Read/Write Files From Disk node. 4. 要检查是否成功,请使用“从磁盘读取/写入文件”节点读取生成的二进制文件。
¥To check that it worked out, use the Read/Write Files From Disk node to read the generated binary file.
显示解决方案
¥??? note "Show me the solution"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | |