在包含R

时间:2017-07-28 16:50:32

标签: r grep qdapregex

在文件中,我想使用 grep 或者可能使用包 qdapRegex ' s rm_between 函数提取包含关键字的整个html代码段,让我们说"折扣率"对于这个例子。具体来说,我希望结果看起来像这段代码:



<P>This is a paragraph containing the words discount rate including other things.</P>
&#13;
&#13;
&#13;

&#13;
&#13;
<TABLE width="400">
  <tr>
    <th>Month</th>
    <th>Savings</th>
  </tr>
  <tr>
    <td>Discount Rate</td>
    <td>10.0%</td>
  </tr>
  <tr>
    <td>February</td>
    <td>$80</td>
  </tr>
</TABLE>
&#13;
&#13;
&#13;

  1. 这里的诀窍是必须首先找到折扣率,然后取出其余部分。
  2. 总是在<P> and </P><TABLE and </TABLE>之间,而不是其他html标签。
  3. 可以在此处找到一个好的示例.txt文件:

    https://www.sec.gov/Archives/edgar/data/66740/0000897101-04-000425.txt

1 个答案:

答案 0 :(得分:1)

您可以将该文件视为html并进行探索,就像使用rvest抓取它一样:

library(rvest)
library(stringr)

# Extract the html from the file
html = read_html('~/Downloads/0000897101-04-000425.txt')

# Get all the 'p' nodes (you can do the same for 'table')
p_nodes <- html %>% html_nodes('p')

# Get the text from each node
p_nodes_text <- p_nodes %>% html_text()

# Find the nodes that have the term you are looking for
match_indeces <- str_detect(p_nodes_text, fixed('discount rate', ignore_case = TRUE))

# Keep only the nodes with matches
# Notice that I remove the first match because rvest adds a 
# 'p' node to the whole file, since it is a text file
match_p_nodes <- p_nodes[match_indeces][-1]

# If you want to see the results, you can print them like this
# (or you could send them to a file)
for(i in 1:length(match_p_nodes)) {
  cat(paste0('Node #', i, ': ', as.character(match_p_nodes[i]), '\n\n'))
}

对于<table>代码,您不会删除第一个匹配项:

table_nodes <- html %>% html_nodes('table')
table_nodes_text <- table_nodes %>% html_text()
match_indeces_table <- str_detect(table_nodes_text, fixed('discount rate', ignore_case = TRUE))
match_table_nodes <- table_nodes[match_indeces_table]

for(i in 1:length(match_table_nodes)) {
  cat(paste0('Node #', i, ': ', as.character(match_table_nodes[i]), '\n\n'))
}