【发布时间】:2015-11-06 23:48:08
【问题描述】:
我正在尝试从网页中抓取 html 表格。但是,该页面包含许多我不想抓取的 html 表。为了识别我要抓取的表,我想使用特定单词组合之后的第一个表(单词组合不在表中,而是文本的一部分)。这是一个例子:
这是我感兴趣的表格:
library(XML)
url <- "http://www.sec.gov/Archives/edgar/data/1301063/000119312514133663/0001193125-14-133663.txt"
readHTMLTable(url, trim = T, header = F, stringsAsFactors = F)[29]
我想用来检测表格的标准是它是遵循此单词组合的第一个表格:
“安全、健康、环境和可持续性挑战”
html <- getURL(url, followlocation = TRUE)
doc <- htmlParse(html, asText = TRUE)
text <- xpathSApply(doc, "//text()[not(ancestor::script)][not(ancestor::style)][not(ancestor::noscript)][not(ancestor::form)]", xmlValue)
grep("safety, health, environmental and sustainability challenges", text, value = T)
【问题讨论】:
标签: html r xpath web-scraping html-table