这个示例展示了如何解析HTML代码并从特定元素中提取文本内容。
从URL读取HTML代码//www.tatmou.com/help/textanalytics
使用webread
.
url =“//www.tatmou.com/help/textanalytics”;代码= webread (url);
使用。解析HTML代码htmlTree
.
树= htmlTree(代码);
查看树的HTML元素名。
树。的名字
ans =“HTML”
查看树的子元素。子树是树
.
树。孩子们
ans = 4×1 htmlTree:Text Analytics Toolbox Documentation < DIV class = "头visible-xs visible-sm " id = " header_mobile "翻译=“不”> <导航类= =“导航”“navbar navbar-default”作用> < DIV class = " container-fluid " > < DIV class = "行" > < DIV class = " col-xs-12 " > < DIV class = " navbar-header " > <按钮类=“navbar-toggle topnav_toggle”数据目标= " # topnav_collapse " data-toggle =“崩溃”type = "按钮" > <跨类=“sr-only”>切换主要导航< / SPAN > < SPAN class = "图标菜单" / > < /按钮> < class = " svg_link navbar-brand " href = " //www.tatmou.com?s_tid=gn_logo " > < IMG alt = " MathWorks " class = " mw_logo " src = " /图片/响应全球/ pic-header-mathworks-logo.svg " / > < / > < / DIV > < / DIV > < / DIV > < DIV class = "行visible-xs visible-sm”> < DIV
要直接从HTML树中提取文本,请使用extractHTMLText
.
str = extractHTMLText(树)
str = "Text Analytics Toolbox™为文本数据的预处理、分析和建模提供了算法和可视化。使用工具箱创建的模型可用于情感分析、预测维护和主题建模等应用程序。文本分析工具箱包括用于处理来自设备日志、新闻源、调查、运营商报告和社交媒体等来源的原始文本的工具。您可以从流行的文件格式中提取文本,预处理原始文本,提取单个单词,将文本转换为数字表示,并构建统计模型。使用机器学习技术,如LSA、LDA和单词嵌入,您可以从高维文本数据集找到聚类并创建特征。文本分析工具箱创建的功能可以与其他数据源的功能相结合,构建利用文本、数字和其他类型数据的机器学习模型。”
要查找HTML树中的特定元素,请使用findElement
.在HTML树中找到所有超链接。在HTML中,超链接使用“A”标签。
选择器=“一个”;子树= findElement(树,选择器);
查看前几个子树。
子树(1:20)
ans = 20×1 htmlTree:< class = " svg_link navbar-brand " href = " //www.tatmou.com?s_tid=gn_logo " > < IMG alt = " MathWorks " class = " mw_logo " src = " /图片/响应全球/ pic-header-mathworks-logo.svg " / > < / > < class = " mwa-nav_login " href = " //www.tatmou.com/login?uri=http: / / www.tatmou.com/help/textanalytics/index.html " >在< / > <迹象产品href = " https://www.mathworks下载188bet金宝搏.com/products.html?s_tid=gn_ps " > < / > < A href = " https://www.mathworks金宝搏官方网站.com/solutions.html?s_tid=gn_sol " >解决方案< / > < A href = " //www.tatmou.com/academia.html?s_tid=gn_acad " >学术界< / > < A href = "金宝app //www.tatmou.com/support.html?s_tid=gn_supp " > < / > <支持社区href = " //www.tatmou.com/matlabcentral/?s_tid=gn_mlc " > < / > < A href = " //www.tatmou.com/company/events.html?s_tid=gn_ev " >事件< / > < A href = " //www.tatmou.com/company/aboutus/contact_us.html?s_tid=gn_cntus " >联系我们< / > < A href = " //www.tatmou.com/store?s_cid=store_top_nav& s_tid = gn_store " > < / > <买href="//www.tatmou.com/au/company/aboutus/contact_us.html?s_tid=gn_cntus">Contact Us How to Buy Sign In Products Solutions Academia Support Community Events
从超链接的文本创建一个单词云。
str = extractHTMLText(子树);图wordcloud (str);标题(“超链接”)
从HTML树中的段落元素中获取类属性。
子树= findElement(树,“p”);attr =“类”;str = getAttribute(子树,attr)
str =21日×1的字符串数组"add_margin_5" "category_desc" "category_desc" "category_desc" "category_desc" "text-center" "copyright"
使用class从段落元素中包含的文本创建单词云“category_desc”
.
子树= findElement(树,“p.category_desc”);str = extractHTMLText(子树);图wordcloud (str);
htmlTree
|findElement
|getAttribute
|extractHTMLText
|tokenizedDocument