メインコンテンツ

このページの内容は最新ではありません。最新版の英語を参照するには、ここをクリックします。

findElement

HTML ツリー内の要素の検出

説明

subtrees = findElement(tree,selector) は、CSS セレクターに一致する tree 内の要素を返します。

すべて折りたたむ

関数 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 で導入