MATLAB Answers

How can I read an HTML file into MATLAB and discard the HTML tags?

156 views (last 30 days)
I have an HTML file that I would like to read into MATLAB. However, I would like to discard the HTML tags and keep only the text from the file.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 4 Jan 2018
There is no function available in MATLAB that will read HTML files and remove the HTML tags. However, this can easily be accomplished by using Regular Expressions:
str = '<HTML>My flowers <b>may</b> <A HREF=''<http://www.a.com'' http://www.a.com''>>bloom in</A> May</HTML>';
pat = '<[^>]*>';
regexprep(str, pat, '')
An alternative method without the use of Regular Expressions involves scanning the HTML file, replacing *"*BR" tags with newline characters, as well as removing other tags. The attached example, fread_html.m, demonstrates one possible solution that handles a subset of HTML tags.
An alternative method of saving an html file without HTML formatting tags is accomplished through the use of ActiveX. The following code calls Microsoft Internet Explorer as an ActiveX automation server, copies the text of the supplied URL and stores the text into a MATLAB variable.
function str=CopyPasteIE(url);
ha = actxserver('internetexplorer.application');
Navigate(ha, url);
pause(3); % Pause to let the page load
ha.document.execCommand('selectall','','');
ha.document.execCommand('copy','','');
str=clipboard('paste');
Example usage: mystr=CopyPasteIE('http://www.google.com');
Note: The code provided in this example is for demonstration purposes and has not been throughly tested.

  3 Comments

Walter Roberson
Walter Roberson on 22 Oct 2017
Many people would not have the Text Analytics Toolbox, which is new as of R2017b and appears to be extra cost. It is important to retain this existing solution.
Rik
Rik on 2 Nov 2017
I agree with Walter on this one. At least keep the old method for people who don't have the Text Analytics Toolbox. Removing the regexp method would imply to future readers that there is no compact method to do this without the toolbox. I don't have the toolbox, so I can't test performance, but I would guess they are not wildly different for most use cases.

Sign in to comment.

More Answers (1)

Sean de Wolski
Sean de Wolski on 17 Oct 2017

  1 Comment

Walter Roberson
Walter Roberson on 22 Oct 2017
That toolbox is new as of R2017b, and it also requires the Statistics and Machine Learning toolbox. If you already have the toolbox for other reasons then certainly use it, but if not then regexp() does fine.

Sign in to comment.


Translated by