【发布时间】:2015-04-15 13:45:45
【问题描述】:
我的目标是在相当大的 Word 文档上使用 library(tm) 工具包。 Word 文档有合理的排版,所以我们有h1 用于主要部分,一些h2 和h3 子标题。我想对每个部分进行比较和文本挖掘(每个 h1 下面的文本 - 副标题并不重要 - 因此可以包含或排除它们。)
我的策略是将 worddocument 导出为 html,然后使用rvestpacakge 提取段落。
library(rvest)
# the file has latin-1 chars
#Sys.setlocale(category="LC_ALL", locale="da_DK.UTF-8")
# small example html file
file <- rvest::html("https://83ae1009d5b31624828197160f04b932625a6af5.googledrive.com/host/0B9YtZi1ZH4VlaVVCTGlwV3ZqcWM/tidy.html", encoding = 'utf-8')
nodes <- file %>%
rvest::html_nodes("h1>p") %>%
rvest::html_text()
我可以提取所有的<p>和html_nodes("p"),但这只是一大汤。我需要分别分析每个h1。
最好的可能是一个列表,每个h1 标题都有一个p 标签向量。并且可能是像for (i in 1:length(html_nodes(fil, "h1"))) (html_children(html_nodes(fil, "h1")[i])) 这样的循环(不起作用)。
如果有办法从rvest 中整理 html 中的单词,则奖励
【问题讨论】:
-
您现在可以使用
htmltidy包(包装libtidy)直接在R 中整理难看的Word 生成的HTML。
标签: r css-selectors web-scraping rvest