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

조회 수: 43 (최근 30일)
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.

채택된 답변

MathWorks Support Team
MathWorks Support Team 2018년 1월 4일
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.
  댓글 수: 5
Walter Roberson
Walter Roberson 2021년 4월 24일
You would probably use websave() or urlread()
Walter Roberson
Walter Roberson 2021년 4월 24일
You might need webread() with some carefully constructed headers to authenticate yourself.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Sean de Wolski
Sean de Wolski 2017년 10월 17일
편집: MathWorks Support Team 2023년 5월 19일
  댓글 수: 1
Walter Roberson
Walter Roberson 2017년 10월 22일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Text Data Preparation에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by