extractHTMLText
HTML からのテキストの抽出
構文
説明
例
HTML からのテキストの抽出
HTML コードからテキスト データを直接抽出するには、HTML コードを string として指定して extractHTMLText
を使用します。
code = "<html><body><h1>THE SONNETS</h1><p>by William Shakespeare</p></body></html>";
str = extractHTMLText(code)
str = "THE SONNETS by William Shakespeare"
Web サイトからのテキストの抽出
Web ページからテキスト データを抽出するには、まず関数 webread
を使用して HTML コードを読み取ります。次に、返されたコードに対して関数 extractHTMLText
を使用します。
url = "https://www.mathworks.com/help/textanalytics";
code = webread(url);
str = extractHTMLText(code)
str = 'Text Analytics Toolbox Analyze and model text data Release Notes PDF Documentation Release Notes PDF Documentation 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. Get Started Learn the basics of Text Analytics Toolbox Text Data Preparation Import text data into MATLAB® and preprocess it for analysis Modeling and Prediction Develop predictive models using topic models and word embeddings Display and Presentation Visualize text data and models using word clouds and text scatter plots Language Support Information on language support in Text Analytics Toolbox'
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"
""
入力引数
code
— HTML コード
string 配列 | 文字ベクトル | 文字ベクトルの cell 配列
HTML コード。string 配列、文字ベクトル、または文字ベクトルの cell 配列として指定します。
ヒント
Web ページから HTML コードを読み取るには、
webread
を使用します。HTML ファイルからテキストを抽出するには、
extractFileText
を使用します。
例: "<a href='https://www.mathworks.com'>MathWorks</a>"
データ型: char
| string
| cell
tree
— HTML ツリー
htmlTree
配列
HTML ツリー。htmlTree
配列として指定します。
ex
— 抽出方法
'tree'
(既定値) | 'article'
| 'all-text'
抽出方法。次のいずれかとして指定します。
オプション | 説明 |
---|---|
'tree' | DOM ツリーとテキスト コンテンツを解析し、段落のブロックを抽出します。 |
'article' | 記事のテキストを検出し、段落のブロックを抽出します。 |
'all-text' | スクリプトと CSS スタイルを除く、HTML 本文のすべてのテキストを抽出します。 |
バージョン履歴
R2018a で導入
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)