%% Parse HTML and Extract Text Content
% This example shows how to parse HTML code and extract the text content from particular elements.
%
%% 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 HTML element name of the tree.

tree.Name

% View the child elements of the tree. The children are subtrees of tree.

tree.Children

%
%% Extract Text from HTML Tree
% To extract text directly from the HTML tree, use extractHTMLText.

str = extractHTMLText(tree)

%
%% Find HTML Elements
% To find particular elements of an HTML tree, use findElement. Find all the hyperlinks in the HTML tree. In HTML, hyperlinks use the "A" tag.

selector = "A";
subtrees = findElement(tree,selector);

% View the first few subtrees.

subtrees(1:20)

% Create a word cloud from the text of the hyperlinks.

str = extractHTMLText(subtrees);
figure
wordcloud(str);
title("Hyperlinks")

%
%% Get HTML Attributes
% Get the class attributes from the paragraph elements in the HTML tree.

subtrees = findElement(tree,'p');
attr = "class";
str = getAttribute(subtrees,attr)

% Create a word cloud from the text contained in paragraph elements with class "category_desc".

subtrees = findElement(tree,'p.category_desc');
str = extractHTMLText(subtrees);
figure
wordcloud(str);

%
% Copyright 2018 The MathWorks, Inc.