7.16.12. html_extract#
Added in version 16.1.6.
Note
This is an experimental feature. Currently, this feature is still not stable.
7.16.12.1. Summary#
html_extract extracts text content from HTML.
It removes HTML tags and expands character references such as <.
html_extract behaves the same as ExtractorHTML.
You can use html_extract to get the text content in HTML as a value.
For example, you can output the main content of an HTML by output_columns or store it to a Generated column.
7.16.12.2. Syntax#
html_extract requires one parameter. options is optional:
html_extract(html)
html_extract(html, options)
options uses the following format.
All of key-value pairs are optional:
{
"selector": selector
}
7.16.12.3. Usage#
Here are a schema definition and sample data to show usage.
Execution example:
table_create Pages TABLE_NO_KEY
# [[0,1337566253.89858,0.000355720520019531],true]
column_create Pages html COLUMN_SCALAR Text
# [[0,1337566253.89858,0.000355720520019531],true]
load --table Pages
[
{"html": "<html><body>He<ll>o</body></html>"},
{"html": "<nav>Menu</nav><article>Hello</article><footer>Footer</footer>"}
]
# [[0,1337566253.89858,0.000355720520019531],2]
Here is an example that extracts text content from HTML.
HTML tags (<html>, <body> and so on) are removed and character references (< and >) are expanded:
Execution example:
select Pages --output_columns 'html_extract(html)'
# [
# [
# 0,
# 1337566253.89858,
# 0.000355720520019531
# ],
# [
# [
# [
# 2
# ],
# [
# [
# "html_extract",
# null
# ]
# ],
# [
# "He<ll>o"
# ],
# [
# "MenuHelloFooter"
# ]
# ]
# ]
# ]
You can extract only the content in the specified HTML tag by selector.
This is useful when you want to extract only the main content of a page.
The first record doesn’t have <article>.
So an empty text is returned for it:
Execution example:
select Pages --output_columns 'html_extract(html, {"selector": "article"})'
# [
# [
# 0,
# 1337566253.89858,
# 0.000355720520019531
# ],
# [
# [
# [
# 2
# ],
# [
# [
# "html_extract",
# null
# ]
# ],
# [
# ""
# ],
# [
# "Hello"
# ]
# ]
# ]
# ]
You can use html_extract in a Generated column.
The following example stores the main content of each HTML to the content column automatically:
Execution example:
column_create Pages content COLUMN_SCALAR Text html \
--generator 'html_extract(html, {"selector": "article"})'
# [[0,1337566253.89858,0.000355720520019531],true]
select Pages --output_columns content
# [
# [
# 0,
# 1337566253.89858,
# 0.000355720520019531
# ],
# [
# [
# [
# 2
# ],
# [
# [
# "content",
# "Text"
# ]
# ],
# [
# ""
# ],
# [
# "Hello"
# ]
# ]
# ]
# ]
7.16.12.4. Parameters#
There is one required parameter. There is one optional parameter.
7.16.12.4.1. Required parameters#
7.16.12.4.1.1. html#
html is the target HTML.
It must be a value of a text type such as ShortText, Text and LongText.
html_extract reports an error if this parameter isn’t a text.
7.16.12.4.2. Optional parameters#
7.16.12.4.2.1. options#
options can be passed as a JSON object.
7.16.12.4.2.1.1. selector#
Specifies elements to extract text content from. Text content out of the elements is ignored.
This is the same as selector of ExtractorHTML. It has the same limitations too. See selector for details.
The default value is no selector. It means that all text content is extracted.
7.16.12.5. Return value#
html_extract returns the extracted text content as a Text.
If selector is specified and no element matches it, html_extract returns an empty text.