htmlTree
Parsed HTML tree
Description
An htmlTree
object represents a parsed HTML element or node.
Extract parts of interest using the findElement
function or the
Children
property, and extract text using the
extractHTMLText
function.
Creation
Syntax
Description
parses the HTML code in the string tree
= htmlTree(code
)code
and returns the resulting
tree structure.
Tip
To parse XML code, use the readstruct
function.
Input Arguments
code
— HTML code
string array | character vector | cell array of character vectors
HTML code, specified as a string array, a character vector, or a cell array of character vectors.
Tip
To read HTML code from a web page, use
webread
.To extract text from an HTML file, use
extractFileText
.
Example: "<a href='https://www.mathworks.com'>MathWorks</a>"
Data Types: char
| string
| cell
Properties
Children
— Direct descendants of element
htmlTree
array
Direct descendants of the element, specified as an htmlTree
array.
Parent
— Parent node
htmlTree
object
Parent node in the tree, specified as an htmlTree
object.
If the HTML tree is a root node, then the value of Parent
is
missing
.
Name
— HTML element name
string scalar
HTML element name, specified as a string scalar.
For more information, see HTML Elements.
Object Functions
findElement | Find elements in HTML tree |
getAttribute | Read HTML attribute of root node of HTML tree |
extractHTMLText | Extract text from HTML |
ismissing | Find HTML trees without values |
Examples
Parse HTML Code
Read HTML code from the URL https://www.mathworks.com/help/textanalytics
using webread
.
url = "https://www.mathworks.com/help/textanalytics";
code = webread(url);
Parse the HTML code using htmlTree
.
tree = htmlTree(code);
View the element name of the root node of the tree.
tree.Name
ans = "HTML"
View the children of the root node.
tree.Children
ans = 4×1 htmlTree: " " <HEAD><TITLE>Text Analytics Toolbox Documentation</TITLE><META charset="utf-8"/><META content="width=device-width, initial-scale=1.0" name="viewport"/><META content="IE=edge" http-equiv="X-UA-Compatible"/><LINK href="/includes_content/responsive/css/bootstrap/bootstrap.min.css" rel="stylesheet" type="text/css"/><LINK href="/includes_content/responsive/css/site6.css?201903" rel="stylesheet" type="text/css"/><LINK href="/includes_content/responsive/css/site6_lg.css?201903" media="screen and (min-width: 1200px)" rel="stylesheet"/><LINK href="/includes_content/responsive/css/site6_md.css?201903" media="screen and (min-width: 992px) and (max-width: 1199px)" rel="stylesheet"/><LINK href="/includes_content/responsive/css/site6_sm+xs.css?201903" media="screen and (max-width: 991px)" rel="stylesheet"/><LINK href="/includes_content/responsive/css/site6_sm.css?201903" media="screen and (min-width: 768px) and (max-width: 991px)" rel="stylesheet"/><LINK href="/includes_content/responsive/css/site6_… " " <BODY id="responsive_offcanvas"><!-- Mobile TopNav: Start --><DIV class="header visible-xs visible-sm" id="header_mobile" translate="no"><NAV class="navbar navbar-default" role="navigation"><DIV class="container-fluid"><DIV class="row"><DIV class="col-xs-12"><DIV class="navbar-header"><BUTTON class="navbar-toggle topnav_toggle" data-target="#topnav_collapse" data-toggle="collapse" type="button"><SPAN class="sr-only">Toggle Main Navigation</SPAN><SPAN class="icon-menu"/></BUTTON><A class="svg_link navbar-brand" href="https://www.mathworks.com?s_tid=gn_logo"><IMG alt="MathWorks" class="mw_logo" src="/images/responsive/global/pic-header-mathworks-logo.svg"/></A></DIV></DIV></DIV><DIV class="row visible-xs visible-sm"><DIV class="col-xs-12"><DIV class="navbar-collapse collapse" id="topnav_collapse"><UL class="nav navbar-nav" id="topnav"><LI class="headernav_login"><A class="mwa-nav_login" href="https://www.mathworks.com/login?uri=http://www.mathworks.com/help/textanalytics/index.html">Sign…
Extract the text from the HTML tree using extractHTMLText
.
str = extractHTMLText(tree)
str = "Text Analytics Toolbox™ provides algorithms and visualizations for preprocessing, analyzing, and modeling text data. Models created with the toolbox can be used in applications such as sentiment analysis, predictive maintenance, and topic modeling. Text Analytics Toolbox includes tools for processing raw text from sources such as equipment logs, news feeds, surveys, operator reports, and social media. You can extract text from popular file formats, preprocess raw text, extract individual words, convert text into numerical representations, and build statistical models. Using machine learning techniques such as LSA, LDA, and word embeddings, you can find clusters and create features from high-dimensional text datasets. Features created with Text Analytics Toolbox can be combined with features from other data sources to build machine learning models that take advantage of textual, numeric, and other types of data."
Find Elements in HTML Tree
Read HTML code from the URL https://www.mathworks.com/help/textanalytics
using the webread
function.
url = "https://www.mathworks.com/help/textanalytics";
code = webread(url);
Parse the HTML code using htmlTree
.
tree = htmlTree(code);
Find all the hyperlinks in the HTML tree using findElement
. The hyperlinks are nodes with element name "A"
.
selector = "A";
subtrees = findElement(tree,selector);
View the first few subtrees.
subtrees(1:10)
ans = 10×1 htmlTree: <A class="skip_link sr-only" href="#content_container">Skip to content</A> <A href="https://www.mathworks.com?s_tid=gn_logo" class="svg_link navbar-brand"><IMG src="/images/responsive/global/pic-header-mathworks-logo.svg" class="mw_logo" alt="MathWorks"/></A> <A href="https://www.mathworks.com/products.html?s_tid=gn_ps">Products</A> <A href="https://www.mathworks.com/solutions.html?s_tid=gn_sol">Solutions</A> <A href="https://www.mathworks.com/academia.html?s_tid=gn_acad">Academia</A> <A href="https://www.mathworks.com/support.html?s_tid=gn_supp">Support</A> <A href="https://www.mathworks.com/matlabcentral/?s_tid=gn_mlc">Community</A> <A href="https://www.mathworks.com/company/events.html?s_tid=gn_ev">Events</A> <A href="https://www.mathworks.com/products/get-matlab.html?s_tid=gn_getml">Get MATLAB</A> <A href="https://www.mathworks.com?s_tid=gn_logo" class="svg_link pull-left"><IMG src="/images/responsive/global/pic-header-mathworks-logo.svg" class="mw_logo" alt="MathWorks"/></A>
Extract the text from the subtrees using extractHTMLText
. The result contains the link text from each link on the page.
str = extractHTMLText(subtrees); str(1:10)
ans = 10×1 string
"Skip to content"
""
"Products"
"Solutions"
"Academia"
"Support"
"Community"
"Events"
"Get MATLAB"
""
Get Attribute of HTML Tag
Read HTML code from the URL https://www.mathworks.com/help/textanalytics
using webread
.
url = "https://www.mathworks.com/help/textanalytics";
code = webread(url);
Parse the HTML code using htmlTree
.
tree = htmlTree(code);
Find all the hyperlinks in the HTML tree using findElement
. The hyperlinks are the nodes with element name "A"
.
selector = "A";
subtrees = findElement(tree,selector);
subtrees(1:10)
ans = 10×1 htmlTree: <A class="svg_link navbar-brand" href="https://www.mathworks.com?s_tid=gn_logo"><IMG alt="MathWorks" class="mw_logo" src="/images/responsive/global/pic-header-mathworks-logo.svg"/></A> <A class="mwa-nav_login" href="https://www.mathworks.com/login?uri=http://www.mathworks.com/help/textanalytics/index.html">Sign In</A> <A href="https://www.mathworks.com/products.html?s_tid=gn_ps">Products</A> <A href="https://www.mathworks.com/solutions.html?s_tid=gn_sol">Solutions</A> <A href="https://www.mathworks.com/academia.html?s_tid=gn_acad">Academia</A> <A href="https://www.mathworks.com/support.html?s_tid=gn_supp">Support</A> <A href="https://www.mathworks.com/matlabcentral/?s_tid=gn_mlc">Community</A> <A href="https://www.mathworks.com/company/events.html?s_tid=gn_ev">Events</A> <A href="https://www.mathworks.com/company/aboutus/contact_us.html?s_tid=gn_cntus">Contact Us</A> <A href="https://www.mathworks.com/store?s_cid=store_top_nav&s_tid=gn_store">How to Buy</A>
Get the hyperlink references using getAttribute
. Specify the attribute name "href"
.
attr = "href";
str = getAttribute(subtrees,attr);
str(1:10)
ans = 10×1 string array
"https://www.mathworks.com?s_tid=gn_logo"
"https://www.mathworks.com/login?uri=http://www.mathworks.com/help/textanalytics/index.html"
"https://www.mathworks.com/products.html?s_tid=gn_ps"
"https://www.mathworks.com/solutions.html?s_tid=gn_sol"
"https://www.mathworks.com/academia.html?s_tid=gn_acad"
"https://www.mathworks.com/support.html?s_tid=gn_supp"
"https://www.mathworks.com/matlabcentral/?s_tid=gn_mlc"
"https://www.mathworks.com/company/events.html?s_tid=gn_ev"
"https://www.mathworks.com/company/aboutus/contact_us.html?s_tid=gn_cntus"
"https://www.mathworks.com/store?s_cid=store_top_nav&s_tid=gn_store"
Convert Parsed HTML Code to String
Read HTML code from the URL https://www.mathworks.com/help/textanalytics
using the webread
function.
url = "https://www.mathworks.com/help/textanalytics";
code = webread(url);
Parse the HTML code using the htmlTree
function.
tree = htmlTree(code);
Find all the paragraphs in the HTML tree using the findElement
function. The paragraphs are the nodes with element name "P".
subtrees = findElement(tree,"P");
Convert the subtrees to string using the string
function.
str = string(subtrees)
str = 18×1 string
"<P class="h1">↵ <A href="../index.html">Help Center</A>↵</P>"
"<P>Text Analytics Toolbox™ provides algorithms and visualizations for preprocessing, analyzing, and modeling text data. Models created with the toolbox can be used in applications such as sentiment analysis, predictive maintenance, and topic modeling.</P>"
"<P>Text Analytics Toolbox includes tools for processing raw text from sources such as equipment logs, news feeds, surveys, operator reports, and social media. You can extract text from popular file formats, preprocess raw text, extract individual words, convert text into numerical representations, and build statistical models.</P>"
"<P>Using machine learning techniques such as LSA, LDA, and word embeddings, you can find clusters and create features from high-dimensional text datasets. Features created with Text Analytics Toolbox can be combined with features from other data sources to build machine learning models that take advantage of textual, numeric, and other types of data.</P>"
"<P class="category_desc">Learn the basics of Text Analytics Toolbox</P>"
"<P class="category_desc">Import text data into MATLAB<SUP>®</SUP> and preprocess it for analysis</P>"
"<P class="category_desc">Develop predictive models using topic models and word embeddings</P>"
"<P class="category_desc">Visualize text data and models using word clouds and text scatter plots</P>"
"<P class="category_desc">Information on language support in Text Analytics Toolbox</P>"
"<P>You clicked a link that corresponds to this MATLAB command:</P>"
"<P>Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.</P>"
"<P class="h1 icon-globe icon_color_secondary" id="country-unselected-title">Select a Web Site</P>"
"<P>Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: <STRONG class="recommended-country"/>.</P>"
"<P>You can also select a web site from the following list:</P>"
"<P>Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.</P>"
"<P class="text-center">↵ <A href="#" class="worldwide_link">Contact your local office</A>↵</P>"
"<P class="copyright" translate="no">© 1994-2024 The MathWorks, Inc.</P>"
"<P>↵ <EM>Join the conversation</EM>↵</P>"
More About
HTML Elements
A typical HTML element contains the following components:
Element name – Name of the HTML tag. The element name corresponds to the
Name
property of the HTML tree.Attributes – Additional information about the tag. HTML attributes have the form
, wherename
="value
"
andname
denote the attribute name and value respectively. The attributes appear inside the opening HTML tag. To get the attribute values from an HTML tree, usevalue
getAttribute
.Content – Element content. The content appears between opening and closing HTML tags. The content can be text data or nested HTML elements. To extract the text from an
htmlTree
object, useextractHTMLText
. To get the nested HTML elements of anhtmlTree
object, use theChildren
property.
For example, the HTML element <a
href="https://www.mathworks.com">Home</a>
comprises the following
components:
Component | Value | Description | |
---|---|---|---|
Element name | a | Element is a hyperlink | |
Attribute | Attribute name | href | Hyperlink reference |
Attribute value | "https://www.mathworks.com" | Hyperlink reference value | |
Content | Home | Text to display |
Version History
Introduced in R2018bR2021a: htmlTree
uses different algorithm for restructuring malformed HTML
When creating an htmlTree
object, the software automatically
restructures malformed input HTML code to have valid structure. This restructuring process
includes adding, removing, and editing elements as well as rearranging the tree structure.
Starting in R2021a, the software uses an updated algorithm to restructure malformed HTML.
This change can result in htmlTree
objects created in R2021a or later
having different size, structure, and content when compared to previous releases.
Starting in R2021a, when loading htmlTree
objects from MAT files
created in an R2020b or before, the software automatically restructures the
htmlTree
object using the same algorithm used for creating
htmlTree
objects. When loading htmlTree
objects from MAT
files created in R2021a or later, the software does not restructure the
htmlTree
object.
This table highlights some notable steps of the restructuring process:
Step | Change in Behavior |
---|---|
Automatically add head and title elements. | Starting in R2021a, when creating an When loading |
Automatically add missing elements. | Starting in R2021a, when creating an When loading
|
Discard parts of malformed code. | When creating an |
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)