extractHTMLText
Extract text from HTML
Syntax
Description
Examples
Extract Text from HTML
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"
Extract Text from Website
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'
Find Elements in HTML Tree
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
code
— HTML code
string array | character vector | cell array of character vectors
HTML code, specified as a string array, character vector, or 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=''>bat365</a>"
Data Types: char
| string
| cell
tree
— HTML tree
htmlTree
array
HTML tree, specified as an htmlTree
array.
ex
— Extraction method
'tree'
(default) | 'article'
| 'all-text'
Extraction method, specified as one of the following:
Option | Description |
---|---|
'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
Open Example
You have a modified version of this example. Do you want to open this example with your edits?
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 bat365 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)