This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

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

tree = htmlTree(code)

Description

example

tree = htmlTree(code) parses the HTML code in the string code and returns the resulting tree structure.

Input Arguments

expand all

HTML code, specified as a string array, a character vector, or a cell array of character vectors.

Tip

Example: "<a href='https://www.mathworks.com'>MathWorks</a>"

Data Types: char | string | cell

Properties

expand all

Direct descendants of the element, specified as an htmlTree array.

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.

HTML element name, specified as a string scalar.

For more information, see HTML Elements.

Object Functions

findElementFind elements in HTML tree
getAttributeRead HTML attribute of root node of HTML tree
extractHTMLTextExtract text from HTML
ismissingFind HTML trees without values

Examples

collapse all

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:

   (1,1)   
   (2,1)  <HEAD><TITLE>Text Analytics Toolbox Documentation</TITLE><META charse…
   (3,1)   
   (4,1)  <BODY id="responsive_offcanvas"><!-- Mobile TopNav: Start --><DIV cla…

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."

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:

   (1,1)  <A class="svg_link navbar-brand" href="https://www.mathworks.com?s_ti…
   (2,1)  <A class="mwa-nav_login" href="https://www.mathworks.com/login?uri=ht…
   (3,1)  <A href="https://www.mathworks.com/products.html?s_tid=gn_ps">Product…
   (4,1)  <A href="https://www.mathworks.com/solutions.html?s_tid=gn_sol">Solut…
   (5,1)  <A href="https://www.mathworks.com/academia.html?s_tid=gn_acad">Acade…
   (6,1)  <A href="https://www.mathworks.com/support.html?s_tid=gn_supp">Suppor…
   (7,1)  <A href="https://www.mathworks.com/matlabcentral/?s_tid=gn_mlc">Commu…
   (8,1)  <A href="https://www.mathworks.com/company/events.html?s_tid=gn_ev">E…
   (9,1)  <A href="https://www.mathworks.com/company/aboutus/contact_us.html?s_…
  (10,1)  <A href="https://www.mathworks.com/store?s_cid=store_top_nav&amp;s_ti…

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 array
    ""
    "Sign In"
    "Products"
    "Solutions"
    "Academia"
    "Support"
    "Community"
    "Events"
    "Contact Us"
    "How to Buy"

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&amp;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"

More About

expand all

Introduced in R2018b