getAttribute
HTML ツリーのルート ノードの 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);
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"
入力引数
tree
— HTML ツリー
htmlTree
配列
HTML ツリー。htmlTree
配列として指定します。
attr
— 属性名
string スカラー | 文字ベクトル | 文字ベクトルを含むスカラー cell 配列
属性名。string スカラー、文字ベクトル、または文字ベクトルを含むスカラー cell 配列として指定します。
出力引数
str
— HTML 属性
string 配列
HTML 属性。string 配列として返されます。
詳細
HTML 要素
一般的な HTML 要素には、次のコンポーネントが含まれます。
要素名 – HTML タグの名前。要素名は、HTML ツリーの
Name
プロパティに対応します。属性 – タグに関する追加情報。HTML 属性の形式は
で、name
="value
"
とname
はそれぞれ属性名と値を示します。属性は HTML の開始タグの内部に出現します。HTML ツリーから属性値を取得するには、value
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 で導入
MATLAB コマンド
次の MATLAB コマンドに対応するリンクがクリックされました。
コマンドを MATLAB コマンド ウィンドウに入力して実行してください。Web ブラウザーは 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)