htmlTree
구문 분석된 HTML 트리
설명
htmlTree
객체는 구문 분석된 HTML 요소 또는 노드를 나타냅니다. findElement
함수 또는 Children
속성을 사용하여 관심 부분을 추출하고 extractHTMLText
함수를 사용하여 텍스트를 추출합니다.
생성
설명
는 문자열 tree
= htmlTree(code
)code
에서 HTML 코드를 구문 분석하고 결과로 생성된 트리 구조체를 반환합니다.
팁
XML 코드를 구문 분석하려면 readstruct
함수를 사용하십시오.
입력 인수
code
— HTML 코드
string형 배열 | 문자형 벡터 | 문자형 벡터로 구성된 셀형 배열
HTML 코드로, string형 배열, 문자형 벡터 또는 문자형 벡터로 구성된 셀형 배열로 지정됩니다.
팁
웹 페이지에서 HTML 코드를 읽어오려면
webread
를 사용하십시오.HTML 파일에서 텍스트를 추출하려면
extractFileText
를 사용하십시오.
예: "<a href='https://www.mathworks.com'>MathWorks</a>"
데이터형: char
| string
| cell
속성
객체 함수
findElement | HTML 트리에서 요소 찾기 |
getAttribute | HTML 트리의 루트 노드의 HTML 특성 읽어오기 |
extractHTMLText | HTML에서 텍스트 추출 |
ismissing | 값이 없는 HTML 트리 찾기 |
예제
HTML 코드 구문 분석하기
webread
를 사용하여 URL https://www.mathworks.com/help/textanalytics
에서 HTML 코드를 읽어옵니다.
url = "https://www.mathworks.com/help/textanalytics";
code = webread(url);
htmlTree
를 사용하여 HTML 코드를 구문 분석합니다.
tree = htmlTree(code);
트리에 있는 루트 노드의 요소 이름을 표시합니다.
tree.Name
ans = "HTML"
루트 노드의 자식을 표시합니다.
tree.Children
ans = 4×1 htmlTree: " " <HEAD><TITLE>Text Analytics Toolbox Documentation</TITLE><META charset="utf-8"/><META content="width=device-width, initial-scale=1.0" name="viewport"/><META content="IE=edge" http-equiv="X-UA-Compatible"/><LINK href="/includes_content/responsive/css/bootstrap/bootstrap.min.css" rel="stylesheet" type="text/css"/><LINK href="/includes_content/responsive/css/site6.css?201903" rel="stylesheet" type="text/css"/><LINK href="/includes_content/responsive/css/site6_lg.css?201903" media="screen and (min-width: 1200px)" rel="stylesheet"/><LINK href="/includes_content/responsive/css/site6_md.css?201903" media="screen and (min-width: 992px) and (max-width: 1199px)" rel="stylesheet"/><LINK href="/includes_content/responsive/css/site6_sm+xs.css?201903" media="screen and (max-width: 991px)" rel="stylesheet"/><LINK href="/includes_content/responsive/css/site6_sm.css?201903" media="screen and (min-width: 768px) and (max-width: 991px)" rel="stylesheet"/><LINK href="/includes_content/responsive/css/site6_… " " <BODY id="responsive_offcanvas"><!-- Mobile TopNav: Start --><DIV class="header visible-xs visible-sm" id="header_mobile" translate="no"><NAV class="navbar navbar-default" role="navigation"><DIV class="container-fluid"><DIV class="row"><DIV class="col-xs-12"><DIV class="navbar-header"><BUTTON class="navbar-toggle topnav_toggle" data-target="#topnav_collapse" data-toggle="collapse" type="button"><SPAN class="sr-only">Toggle Main Navigation</SPAN><SPAN class="icon-menu"/></BUTTON><A class="svg_link navbar-brand" href="https://www.mathworks.com?s_tid=gn_logo"><IMG alt="MathWorks" class="mw_logo" src="/images/responsive/global/pic-header-mathworks-logo.svg"/></A></DIV></DIV></DIV><DIV class="row visible-xs visible-sm"><DIV class="col-xs-12"><DIV class="navbar-collapse collapse" id="topnav_collapse"><UL class="nav navbar-nav" id="topnav"><LI class="headernav_login"><A class="mwa-nav_login" href="https://www.mathworks.com/login?uri=http://www.mathworks.com/help/textanalytics/index.html">Sign…
extractHTMLText
를 사용하여 HTML 트리에서 텍스트를 추출합니다.
str = extractHTMLText(tree)
str = "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."
HTML 트리에서 요소 찾기
webread
함수를 사용하여 URL https://www.mathworks.com/help/textanalytics
에서 HTML 코드를 읽어옵니다.
url = "https://www.mathworks.com/help/textanalytics";
code = webread(url);
htmlTree
를 사용하여 HTML 코드를 구문 분석합니다.
tree = htmlTree(code);
findElement
를 사용하여 HTML 트리에서 하이퍼링크를 모두 찾습니다. 하이퍼링크는 요소 이름이 "A"
인 노드입니다.
selector = "A";
subtrees = findElement(tree,selector);
처음 몇 개의 하위 트리를 표시합니다.
subtrees(1:10)
ans = 10×1 htmlTree: <A class="skip_link sr-only" href="#content_container">Skip to content</A> <A href="https://www.mathworks.com?s_tid=gn_logo" class="svg_link navbar-brand"><IMG src="/images/responsive/global/pic-header-mathworks-logo.svg" class="mw_logo" alt="MathWorks"/></A> <A href="https://www.mathworks.com/products.html?s_tid=gn_ps">Products</A> <A href="https://www.mathworks.com/solutions.html?s_tid=gn_sol">Solutions</A> <A href="https://www.mathworks.com/academia.html?s_tid=gn_acad">Academia</A> <A href="https://www.mathworks.com/support.html?s_tid=gn_supp">Support</A> <A href="https://www.mathworks.com/matlabcentral/?s_tid=gn_mlc">Community</A> <A href="https://www.mathworks.com/company/events.html?s_tid=gn_ev">Events</A> <A href="https://www.mathworks.com/products/get-matlab.html?s_tid=gn_getml">Get MATLAB</A> <A href="https://www.mathworks.com?s_tid=gn_logo" class="svg_link pull-left"><IMG src="/images/responsive/global/pic-header-mathworks-logo.svg" class="mw_logo" alt="MathWorks"/></A>
extractHTMLText
를 사용하여 하위 트리에서 텍스트를 추출합니다. 추출한 결과에 해당 페이지에 있는 각 링크의 링크 텍스트가 들어 있습니다.
str = extractHTMLText(subtrees); str(1:10)
ans = 10×1 string
"Skip to content"
""
"Products"
"Solutions"
"Academia"
"Support"
"Community"
"Events"
"Get MATLAB"
""
HTML 태그의 특성 가져오기
webread
를 사용하여 URL https://www.mathworks.com/help/textanalytics
에서 HTML 코드를 읽어옵니다.
url = "https://www.mathworks.com/help/textanalytics";
code = webread(url);
htmlTree
를 사용하여 HTML 코드를 구문 분석합니다.
tree = htmlTree(code);
findElement
를 사용하여 HTML 트리에서 하이퍼링크를 모두 찾습니다. 하이퍼링크는 요소 이름이 "A"
인 노드입니다.
selector = "A";
subtrees = findElement(tree,selector);
subtrees(1:10)
ans = 10×1 htmlTree: <A class="svg_link navbar-brand" href="https://www.mathworks.com?s_tid=gn_logo"><IMG alt="MathWorks" class="mw_logo" src="/images/responsive/global/pic-header-mathworks-logo.svg"/></A> <A class="mwa-nav_login" href="https://www.mathworks.com/login?uri=http://www.mathworks.com/help/textanalytics/index.html">Sign In</A> <A href="https://www.mathworks.com/products.html?s_tid=gn_ps">Products</A> <A href="https://www.mathworks.com/solutions.html?s_tid=gn_sol">Solutions</A> <A href="https://www.mathworks.com/academia.html?s_tid=gn_acad">Academia</A> <A href="https://www.mathworks.com/support.html?s_tid=gn_supp">Support</A> <A href="https://www.mathworks.com/matlabcentral/?s_tid=gn_mlc">Community</A> <A href="https://www.mathworks.com/company/events.html?s_tid=gn_ev">Events</A> <A href="https://www.mathworks.com/company/aboutus/contact_us.html?s_tid=gn_cntus">Contact Us</A> <A href="https://www.mathworks.com/store?s_cid=store_top_nav&s_tid=gn_store">How to Buy</A>
getAttribute
를 사용하여 하이퍼링크 참조를 가져옵니다. 특성 이름 "href"
를 지정합니다.
attr = "href";
str = getAttribute(subtrees,attr);
str(1:10)
ans = 10×1 string array
"https://www.mathworks.com?s_tid=gn_logo"
"https://www.mathworks.com/login?uri=http://www.mathworks.com/help/textanalytics/index.html"
"https://www.mathworks.com/products.html?s_tid=gn_ps"
"https://www.mathworks.com/solutions.html?s_tid=gn_sol"
"https://www.mathworks.com/academia.html?s_tid=gn_acad"
"https://www.mathworks.com/support.html?s_tid=gn_supp"
"https://www.mathworks.com/matlabcentral/?s_tid=gn_mlc"
"https://www.mathworks.com/company/events.html?s_tid=gn_ev"
"https://www.mathworks.com/company/aboutus/contact_us.html?s_tid=gn_cntus"
"https://www.mathworks.com/store?s_cid=store_top_nav&s_tid=gn_store"
구문 분석된 HTML 코드를 string형으로 변환하기
webread
함수를 사용하여 URL https://www.mathworks.com/help/textanalytics
에서 HTML 코드를 읽어옵니다.
url = "https://www.mathworks.com/help/textanalytics";
code = webread(url);
htmlTree
함수를 사용하여 HTML 코드를 구문 분석합니다.
tree = htmlTree(code);
findElement
함수를 사용하여 HTML 트리에서 단락을 모두 찾습니다. 단락은 요소 이름이 "P"인 노드입니다.
subtrees = findElement(tree,"P");
string
함수를 사용하여 하위 트리를 string형으로 변환합니다.
str = string(subtrees)
str = 26×1 string
"<P class="h1">↵ <A href="../index.html" class="coming_from_product">Documentation</A>↵ <A href="../index.html" class="not_coming_from_product">Help Center</A>↵</P>"
"<P>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.</P>"
"<P>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.</P>"
"<P>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.</P>"
"<P class="category_desc">Learn the basics of Text Analytics Toolbox</P>"
"<P class="category_desc">Import text data into MATLAB<SUP>®</SUP> and preprocess it for analysis</P>"
"<P class="category_desc">Develop predictive models using topic models and word embeddings</P>"
"<P class="category_desc">Visualize text data and models using word clouds and text scatter plots</P>"
"<P class="category_desc">Information on language support in Text Analytics Toolbox</P>"
"<P>You clicked a link that corresponds to this MATLAB command:</P>"
"<P>Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.</P>"
"<P>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: <STRONG class="recommended-country"/>.</P>"
"<P>You can also select a web site from the following list:</P>"
"<P>Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.</P>"
"<P class="text-center">↵ <A href="#" class="worldwide_link">Contact your local office</A>↵</P>"
"<P class="ff_section_title">Explore Products</P>"
"<P class="ff_section_title">Try or Buy</P>"
"<P class="ff_section_title">Learn to Use</P>"
"<P class="ff_section_title">Get Support</P>"
"<P class="ff_section_title">About <SPAN translate="no">MathWorks</SPAN></P>"
"<P class="h4 add_font_futura_medium add_margin_0">↵ <SPAN translate="no">MathWorks</SPAN>↵</P>"
"<P>↵ <EM>Accelerating the pace of engineering and science</EM>↵</P>"
"<P><SPAN translate="no">MathWorks</SPAN> is the leading developer of mathematical computing software for engineers and scientists.</P>"
"<P>↵ <A href="/discovery.html?s_tid=all_disc_mw_ff">Discover...</A>↵</P>"
"<P class="copyright" translate="no">© 1994-2021 The MathWorks, Inc.</P>"
"<P>↵ <EM>Join the conversation</EM>↵</P>"
세부 정보
HTML 요소
일반적인 HTML 요소에는 다음과 같은 성분이 들어 있습니다.
요소 이름 – HTML 태그의 이름. 요소 이름은 HTML 트리의
Name
속성에 해당합니다.특성 – 태그에 대한 추가 정보. HTML 특성은
형식입니다. 여기서name
="value
"
과name
는 각각 특성 이름과 특성 값을 나타냅니다. 특성은 여는 HTML 태그 안에 있습니다. HTML 트리에서 특성 값을 가져오려면value
getAttribute
를 사용하십시오.내용 – 요소 내용. 내용은 여는 HTML 태그와 닫는 HTML 태그 사이에 있습니다. 내용은 텍스트 데이터이거나 중첩된 HTML 요소일 수 있습니다.
htmlTree
객체에서 텍스트를 추출하려면extractHTMLText
를 사용하십시오.htmlTree
객체의 중첩된 HTML 요소를 가져오려면Children
속성을 사용하십시오.
예를 들어 HTML 요소 <a href="https://www.mathworks.com">Home</a>
은 다음 성분으로 이루어져 있습니다.
성분 | 값 | 설명 | |
---|---|---|---|
요소 이름 | a | 하이퍼링크에 해당하는 요소 | |
특성 | 특성 이름 | href | 하이퍼링크 참조 |
특성 값 | "https://www.mathworks.com" | 하이퍼링크 참조 값 | |
내용 | Home | 표시할 텍스트 |
버전 내역
R2018b에 개발됨R2021a: htmlTree
는 형식이 잘못된 HTML을 재구성하는 데 다른 알고리즘을 사용함
htmlTree
객체를 만들 때 형식이 잘못된 입력 HTML 코드의 경우 유효한 구조를 갖도록 자동으로 재구성합니다. 재구성하는 과정에서 트리 구조의 재배열뿐만 아니라 요소의 추가, 제거 및 편집도 수행됩니다. R2021a부터는 형식이 잘못된 HTML을 재구성하는 데 업데이트된 알고리즘을 사용합니다. 이 변경으로 인해 R2021a 이상에서 생성된 htmlTree
객체는 크기, 구조 및 내용이 이전 릴리스와 다를 수 있습니다.
R2021a부터는, R2020b 이하 버전에서 생성된 MAT 파일에서 htmlTree
객체를 불러올 때 htmlTree
객체 생성에 사용한 동일한 알고리즘을 사용하여 htmlTree
객체를 자동으로 재구성합니다. R2021a 이상 버전에서 생성된 MAT 파일에서 htmlTree
객체를 불러올 때는 htmlTree
객체를 재구성하지 않습니다.
이 테이블은 재구성 과정의 일부 중요 단계를 중점적으로 설명합니다.
단계 | 동작 변경 |
---|---|
head 및 title 요소를 자동으로 추가. | R2021a부터는 HTML 코드에서 이전 릴리스에서 생성된 MAT 파일에서 |
누락된 요소를 자동으로 추가. | R2021a부터는 HTML 코드에서 이전 릴리스에서 생성된 MAT 파일에서 |
형식이 잘못된 코드 부분을 버림. | 형식이 잘못된 HTML 코드를 사용하여 |
MATLAB 명령
다음 MATLAB 명령에 해당하는 링크를 클릭했습니다.
명령을 실행하려면 MATLAB 명령 창에 입력하십시오. 웹 브라우저는 MATLAB 명령을 지원하지 않습니다.
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 MathWorks 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)