Main Content

extractHTMLText

HTML からのテキストの抽出

説明

str = extractHTMLText(code)code 内の HTML コードを解析して、テキストを抽出します。

str = extractHTMLText(tree) は、HTML ツリーからテキストを抽出します。

str = extractHTMLText(___,'ExtractionMethod',ex) は、使用する抽出方法も指定します。

すべて折りたたむ

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 ページからテキスト データを抽出するには、まず関数 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'

関数 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 コード。string 配列、文字ベクトル、または文字ベクトルの cell 配列として指定します。

ヒント

  • Web ページから HTML コードを読み取るには、webread を使用します。

  • HTML ファイルからテキストを抽出するには、extractFileText を使用します。

例: "<a href='https://www.mathworks.com'>MathWorks</a>"

データ型: char | string | cell

HTML ツリー。htmlTree 配列として指定します。

抽出方法。次のいずれかとして指定します。

オプション説明
'tree'DOM ツリーとテキスト コンテンツを解析し、段落のブロックを抽出します。
'article'記事のテキストを検出し、段落のブロックを抽出します。
'all-text'スクリプトと CSS スタイルを除く、HTML 本文のすべてのテキストを抽出します。

バージョン履歴

R2018a で導入