接收标签后的文字

Receive the text after tag

这提供了 h1 标签内的文本

library(rvest)
df1 <- data.frame(id = c(1,2), text = c("<h1> Title </h1> keep me here <h1>not </h1> or <h2> else</h2>","also not here <h1> but</h1> here also")
rawHTML <- paste(df1$text[1], collapse="\n")
rawHTML %>% read_html() %>% html_nodes("h1") %>% html_text()

有人应该向命令添加什么以接收带有文本标题的标签 h1 之后的内容?

示例输出: 让我留在这里

由于您的 text 是字符串,您可以使用 regex 来提取您需要的数据。

stringr::str_extract(df1$text, '(?<=Title </h1>\s).*(?=\s<h1>)')
#[1] "keep me here" NA    

您也可以使用 str_match 而不使用 lookbehind 和 lookahead 正则表达式,这可能更简单。

stringr::str_match(df1$text, 'Title </h1>\s(.*)\s<h1>')[, 2]

base R中我们可以使用regmatches/gregexpr

with(df1, trimws(sapply(regmatches(text,
    gregexpr("(?<=Title </h1> )[^<]+", text, perl = TRUE)), `[`, 1)))
#[1] "keep me here" NA