주요 콘텐츠

findElement

HTML 트리에서 요소 찾기

설명

subtrees = findElement(tree,selector)tree에서 CSS 선택자와 일치하는 요소를 반환합니다.

예제

예제

모두 축소

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 트리로, 스칼라 htmlTree 객체로 지정됩니다.

CSS 선택자로, string형 스칼라 또는 문자형 벡터로 지정됩니다. 자세한 내용은 CSS 선택자 항목을 참조하십시오.

출력 인수

모두 축소

일치하는 HTML 하위 트리로, htmlTree 배열로 반환됩니다.

세부 정보

모두 축소

참고 문헌

버전 내역

R2018b에 개발됨