Main Content

extractHTMLText

Extract text from HTML

Description

example

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

example

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

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

Examples

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 = 
    "THE SONNETS
     
     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 = "/help/textanalytics";
code = webread(url);
str = extractHTMLText(code)
str = 
    'Text Analytics Toolbox
     
     Analyze and model text data 
     
     Release Notes
     
     PDF Documentation
     
     Release Notes
     
     PDF Documentation
     
     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.
     
     Get Started
     
     Learn the basics of Text Analytics Toolbox
     
     Text Data Preparation
     
     Import text data into MATLAB® and preprocess it for analysis
     
     Modeling and Prediction
     
     Develop predictive models using topic models and word embeddings
     
     Display and Presentation
     
     Visualize text data and models using word clouds and text scatter plots
     
     Language Support
     
     Information on language support in Text Analytics Toolbox'

Read HTML code from the URL /help/textanalytics using the webread function.

url = "/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="/help/textanalytics/ref/s_tid_gn_logo.html" class="svg_link navbar-brand"><IMG src="/images/responsive/global/pic-header-bat365-logo.svg" class="mw_logo" alt="bat365"/></A>
    <A href="/products_s_tid_gn_ps.html">Products</A>
    <A href="/solutions_s_tid_gn_sol.html">Solutions</A>
    <A href="/academia_s_tid_gn_acad.html">Academia</A>
    <A href="/support.html?s_tid=gn_supp">Support</A>
    <A href="/matlabcentral/s_tid_gn_mlc.html">Community</A>
    <A href="/company/events_s_tid_gn_ev.html">Events</A>
    <A href="/products/get-matlab.html?s_tid=gn_getml">Get MATLAB</A>
    <A href="/help/textanalytics/ref/s_tid_gn_logo.html" class="svg_link pull-left"><IMG src="/images/responsive/global/pic-header-bat365-logo.svg" class="mw_logo" alt="bat365"/></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"
    ""

Input Arguments

collapse all

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

Tip

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

Data Types: char | string | cell

HTML tree, specified as an htmlTree array.

Extraction method, specified as one of the following:

OptionDescription
'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.

Version History

Introduced in R2018a