Main Content

getAttribute

HTML ツリーのルート ノードの HTML 属性の読み取り

説明

str = getAttribute(tree,attr) は、tree のルート ノードの属性 attr を返します。その属性が設定されていない場合、関数は missing 値を返します。

すべて折りたたむ

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&amp;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 ツリー。htmlTree 配列として指定します。

属性名。string スカラー、文字ベクトル、または文字ベクトルを含むスカラー cell 配列として指定します。

出力引数

すべて折りたたむ

HTML 属性。string 配列として返されます。

詳細

すべて折りたたむ

HTML 要素

一般的な HTML 要素には、次のコンポーネントが含まれます。

  • 要素名 – HTML タグの名前。要素名は、HTML ツリーの Name プロパティに対応します。

  • 属性 – タグに関する追加情報。HTML 属性の形式は name="value" で、namevalue はそれぞれ属性名と値を示します。属性は HTML の開始タグの内部に出現します。HTML ツリーから属性値を取得するには、getAttribute を使用します。

  • コンテンツ – 要素のコンテンツ。コンテンツは HTML の開始タグと終了タグの間に出現します。コンテンツは、テキスト データまたは入れ子の HTML 要素です。htmlTree オブジェクトからテキストを抽出するには、extractHTMLText を使用します。htmlTree オブジェクトの入れ子の HTML 要素を取得するには、Children プロパティを使用します。

たとえば、HTML 要素 <a href="https://www.mathworks.com">Home</a> は、次のコンポーネントで構成されます。

コンポーネント説明
要素名a要素はハイパーリンク
属性属性名hrefハイパーリンク参照
属性値"https://www.mathworks.com"ハイパーリンク参照の値
コンテンツHome表示するテキスト

バージョン履歴

R2018b で導入