Retrieve data from HTML text
Read information from HTML, such as headings, links or table rows. Use extraction rules to choose the information you want to keep.
Application
- HTML
Inputs (what you have)
| Name | Description | Data Type | Required? | Example |
|---|---|---|---|---|
| HTML text | The HTML text to read. | Text (Long) | Yes | |
| Data extraction rules | Rules naming the information to collect. For example, {"title": "h1"} gets the first heading. See the examples below. | Text (Long) | Yes |
Outputs (what you get)
| Name | Description | Data Type | Required? | Example |
|---|---|---|---|---|
| Extracted content | The selected information, grouped under the names in your rules, as JSON text. | Text (Long) | No |
Outcomes
| Name | Description |
|---|---|
| Success | This status is selected if the job has successfully completed. |
| No result | This status is selected if the job has successfully completed but no result was produced. |
Configuration Rules
A simple example
To collect a heading, start with a rule like this:
| Field | Example |
|---|---|
| HTML text | <h1>Welcome</h1> |
| Data extraction rules | {"heading": "h1"} |
| Extracted content | {"heading": "Welcome"} |
heading is the name you choose for the result. h1 tells the action to read the first main heading in the HTML.
Finding a selector on a webpage
You do not have to write every selector by hand. If your HTML comes from a website, the jQuery Unique Selector Chrome extension can help you find the part you want to read.
- Install the extension and open the webpage containing the information you want.
- Open the extension and click its magnifying glass to turn on selection mode. Click the heading, link or other item you want to extract.
- Copy the Selected Element Selector and use it in your data extraction rules. For example, if it gives you
.product-price, use{"price": ".product-price"}to read that element's text. - Repeat for any other information you need. Click the magnifying glass again to turn selection mode off.
Check the selector against the HTML text supplied to this action. A live webpage may include content added by scripts that is missing from your supplied HTML.
When information is missing
The action can finish successfully even when some information is not found. The value returned depends on the rule you use:
| What happens | Extracted value |
|---|---|
Short text rule, such as "title": ".absent"; no element matches | null |
Rule with separate selector and "output": "text" settings; nothing matches | {} |
Rule asking for a link address ("output": "@href"); nothing matches | {} |
A link is found, but it has no address (href) | "" |
A link is found, but its address (href) is blank | "" |
| An element is found, but its text is blank | "" |
A rule asking for a list ("type": "list"); nothing matches | [] |
A short rule returns null when nothing matches. A rule with separate selector and output settings returns {} instead. This also applies to rules inside other rules. A list rule returns one entry for each match, so one link without an address gives [""]: a list with one blank entry.
For example, use this HTML:
<p>control</p>
<a id="present">Present</a>
<a id="empty" href="">Empty</a>
<span id="blank"></span>
With these data extraction rules:
{
"missing_short": ".absent",
"missing_text": {"selector": ".absent", "output": "text"},
"missing_href": {"selector": ".absent", "output": "@href"},
"present_no_href": {"selector": "#present", "output": "@href"},
"empty_href": {"selector": "#empty", "output": "@href"},
"empty_text": "#blank",
"missing_list": {"selector": ".absent", "type": "list", "output": "text"},
"present_text": {"selector": "p", "output": "text"}
}
The JSON in Extracted content is:
{
"missing_short": null,
"missing_text": {},
"missing_href": {},
"present_no_href": "",
"empty_href": "",
"empty_text": "",
"missing_list": [],
"present_text": "control"
}
Success means the rules were processed; it does not guarantee every value was found.
| Rules supplied | Outcome |
|---|---|
| One or more named rules, even if nothing matches | Success, with the missing values shown above |
No rules ({}) | No result |
Before using the result in another step, check the information you need. Depending on your rule, missing information can appear as null, {}, "" or []. Checking only for null will miss the other cases.
A link without an address and a link with a blank address both return the same blank value.
Writing CSS extraction rules
Selectors match the supplied HTML text. This action does not open a browser or run page scripts, so content must already be present in that HTML. These examples describe CSS rules; XPath selectors have different matching behavior.
Each rule has a name you choose, such as heading or link. The result uses those same names.
| Setting | What it does |
|---|---|
selector | Identifies the part of the HTML to read, such as h1 for a heading or a for a link |
type | Use "item" for the first match (the default), or "list" for all matches |
output | Chooses what to collect: "text" for the text (the default), or an attribute such as "@href" for a link address |
A short rule such as {"heading": "h1"} gets the first heading as text. Use separate settings when you need more control:
| Purpose | Data extraction rules |
|---|---|
| First heading's text | {"heading": "h1"} |
| Text from an element by ID | {"subtitle": "#subtitle"} |
| Explicit first-match text rule | {"heading": {"selector": "h1", "type": "item", "output": "text"}} |
| All matching titles | {"titles": {"selector": ".post-title", "type": "list", "output": "text"}} |
First link's href attribute | {"link": {"selector": "a", "output": "@href"}} |
All links' href attributes | {"links": {"selector": "a", "type": "list", "output": "@href"}} |
| First heading as HTML, including its element tag | {"heading_html": {"selector": "h1", "output": "html"}} |
| Heading's ID attribute | {"heading_id": {"selector": "h1", "output": "@id"}} |
| Heading and first link together | {"heading": "h1", "link": {"selector": "a", "output": "@href"}} |
| First matching post title | {"first_title": {"selector": ".post-title", "type": "item"}} |
| Text inside a card | {"description": ".card > div"} |
| All text inside the page body | {"text": "body"} |
| Addresses from email links | {"email_addresses": {"selector": "a[href^='mailto:']", "type": "list", "output": "@href"}} |
| Table rows as objects keyed by column headers | {"rows": {"selector": "table", "output": "table_json"}} |
| Table as arrays of cell text, starting with a header row | {"rows": {"selector": "table", "output": "table_array"}} |
| A specific table, selected by ID | {"rows": {"selector": "#table_id", "output": "table_json"}} |
| A specific table as arrays of cell text | {"rows": {"selector": "#table_id", "output": "table_array"}} |
Table extraction supports standard <table> markup and ARIA grids with row, columnheader and gridcell roles. Select the grid container instead of table when using an ARIA grid.
Text extraction trims leading and trailing whitespace with both "clean": true and "clean": false. This setting does not preserve the spaces around the extracted text.
You can group rules to collect related information together. In this example, each article gets its own title and link:
{
"articles": {
"selector": "article",
"type": "list",
"output": {
"title": "h2",
"link": {"selector": "a", "output": "@href"},
"description": ".post-description"
}
}
}
With this HTML:
<article>
<h2>First post</h2>
<a href="/first">Read more</a>
<p class="post-description">First post summary</p>
</article>
<article>
<h2>Second post</h2>
<a href="/second">Read more</a>
<p class="post-description">Second post summary</p>
</article>
The extracted content is:
{
"articles": [
{
"title": "First post",
"link": "/first",
"description": "First post summary"
},
{
"title": "Second post",
"link": "/second",
"description": "Second post summary"
}
]
}
Link addresses are returned exactly as written in the HTML. For example, /first stays /first; the website address is not added.
Collecting text and an address for each link
To keep each link's text and address together, use a nested rule:
{
"links": {
"selector": "a",
"type": "list",
"output": {
"text": {"selector": "@", "output": "text"},
"address": {"selector": "@", "output": "@href"}
}
}
}
Inside these nested rules, "selector": "@" reads the current link. Using "selector": "a" there would search for another link inside it.
With <a href="/first">Read more</a>, the result is:
{"links": [{"text": "Read more", "address": "/first"}]}
Reading an HTML attribute
To extract an HTML attribute, put the element selector in selector and the attribute name, prefixed with @, in output. Do not append the attribute to the selector (for example, a@href); that syntax produces an error.
For example, with this HTML:
<a href="https://example.com/page">Example</a>
Use these data extraction rules:
{"link": {"selector": "a", "output": "@href"}}
The extracted content is:
{"link": "https://example.com/page"}