Main Content


Extract text from HTML



str = extractHTMLText(code) parses the HTML code in code and extracts the text.


str = extractHTMLText(tree) extracts the text from an HTML tree.

str = extractHTMLText(___,'ExtractionMethod',ex) also specifies the extraction method to use.


collapse all

To extract text data directly from HTML code, use extractHTMLText and specify the HTML code as a string.

code = "<html><body><h1>THE SONNETS</h1><p>by William Shakespeare</p></body></html>";
str = extractHTMLText(code)
str = 
     by William Shakespeare"

To extract the text data from a web page, first use the webread function to read the HTML code. Then use the extractHTMLText function on the returned code.

url = "";
code = webread(url);
str = extractHTMLText(code)
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 using the webread function.

url = "";
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.

ans = 
  10×1 htmlTree:

    <A class="svg_link navbar-brand" href=""><IMG alt="MathWorks" class="mw_logo" src="/images/responsive/global/pic-header-mathworks-logo.svg"/></A>
    <A href="">Products</A>
    <A href="">Solutions</A>
    <A href="">Academia</A>
    <A href="">Support</A>
    <A href="">Community</A>
    <A href="">Events</A>
    <A href="">Contact Us</A>
    <A href="">Get MATLAB</A>
    <A class="svg_link pull-left" href=""><IMG alt="MathWorks" class="mw_logo" src="/images/responsive/global/pic-header-mathworks-logo.svg"/></A>

Extract the text from the subtrees using extractHTMLText. The result contains the link text from each link on the page.

str = extractHTMLText(subtrees);
ans = 10×1 string
    "Contact Us"
    "Get MATLAB"

Input Arguments

collapse all

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


Example: "<a href=''>MathWorks</a>"

Data Types: char | string | cell

HTML tree, specified as an htmlTree array.

Extraction method, specified as one of the following:

'tree'Analyze the DOM tree and text contents, then extract a block of paragraphs.
'article'Detect article text and extract a block of paragraphs.
'all-text'Extract all text in the HTML body, except for scripts and CSS styles.

Introduced in R2018a