0

在一个文件中,我想使用grep或者可能使用包qdapRegexrm_between函数来提取包含关键字的整个 html 代码部分,让我们说这个例子的“折扣率”。具体来说,我想要看起来像这个代码片段的结果:

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

<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>

  1. 这里的诀窍是它必须先找到贴现率,然后再提取其余部分。
  2. 它总是介于<P> and </P><TABLE and </TABLE>没有其他 html 标记之间。

可以在这里找到一个很好的示例 .txt 文件:

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

4

1 回答 1

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'))
}
于 2017-07-28T17:53:25.777 回答