在 Xpath 中同时转义双引号和单引号

Tha*_*uys 6 quotes xpath r escaping rvest

类似于How to deal with single quote in xpath,我想转义单引号。不同之处在于我不能排除双引号也可能出现在目标字符串中的可能性。

目标:

使用 Xpath(在 R 中)同时转义双引号和单引号。目标元素应用作变量,而不是像现有答案之一那样进行硬编码。(它应该是一个变量,因为我事先不知道内容,它可能有单引号、双引号或两者都有)。

作品:

library(rvest)
library(magrittr)
html <- "<div>1</div><div>Father's son</div>"
target <- "Father's son"
html %>% xml2::read_html() %>% html_nodes(xpath = paste0("//*[contains(text(), \"", target,"\")]"))
{xml_nodeset (1)}
[1] <div>Father's son</div>
Run Code Online (Sandbox Code Playgroud)

不起作用:

html <- "<div>1</div><div>Fat\"her's son</div>"
target <- "Fat\"her's son"
html %>% xml2::read_html() %>% html_nodes(xpath = paste0("//*[contains(text(), \"", target,"\")]"))
{xml_nodeset (0)}
Warning message:
In xpath_search(x$node, x$doc, xpath = xpath, nsMap = ns, num_results = Inf) :
  Invalid expression [1207]
Run Code Online (Sandbox Code Playgroud)

更新

我可以尝试“翻译为 R”的非 R 答案非常受欢迎。

小智 6

因为您使用字符串操作来构建 XPath 表达式,所以表达式是有效的 XPath 是您的责任。这个表达:

//*[contains(.,concat('Fat"',"her's son"))]
Run Code Online (Sandbox Code Playgroud)

选择:

<div>Fat"her's son</div>
Run Code Online (Sandbox Code Playgroud)

这里测试

使用 XPath 字符串变量会是一种更好的方法,但看起来 R 没有 API,即使使用 libxml。


All*_*ron 6

这里的关键是意识到使用 xml2,您可以使用 html 转义字符写回解析后的 html。这个函数可以解决问题。它比它需要的要长,因为我已经包含了注释和一些类型检查/转换逻辑。

contains_text <- function(node_set, find_this)
{
  # Ensure we have a nodeset
  if(all(class(node_set) == c("xml_document", "xml_node")))
    node_set %<>% xml_children()

  if(class(node_set) != "xml_nodeset")
    stop("contains_text requires an xml_nodeset or xml_document.")

  # Get all leaf nodes
  node_set %<>% xml_nodes(xpath = "//*[not(*)]")

  # HTML escape the target string
  find_this %<>% {gsub("\"", "&quot;", .)}

  # Extract, HTML escape and replace the nodes
  lapply(node_set, function(node) xml_text(node) %<>% {gsub("\"", "&quot;", .)})

  # Now we can define the xpath and extract our target nodes
  xpath <- paste0("//*[contains(text(), \"", find_this, "\")]")
  new_nodes <- html_nodes(node_set, xpath = xpath)

  # Since the underlying xml_document is passed by pointer internally,
  # we should unescape any text to leave it unaltered
  xml_text(node_set) %<>% {gsub("&quot;", "\"", .)}
  return(new_nodes)
}
Run Code Online (Sandbox Code Playgroud)

现在:

library(rvest)
library(xml2)

html %>% xml2::read_html() %>% contains_text(target)
#> {xml_nodeset (1)}
#> [1] <div>Fat"her's son</div>
html %>% xml2::read_html() %>% contains_text(target) %>% xml_text()
#> [1] "Fat\"her's son"
Run Code Online (Sandbox Code Playgroud)

附录

这是一种替代方法,它是@Alejandro 建议的方法的实现,但允许任意目标。它的优点是不影响 xml 文档,并且比上述方法快一点,但涉及 xml 库应该阻止的那种字符串解析。它的工作原理是获取目标,在每个"和之后拆分它',然后将每个片段包含在与其包含的引用类型相反的引用类型中,然后用逗号将它们全部粘贴回一起并将它们插入到 XPathconcatenate函数中。

library(stringr)

safe_xpath <- function(target)
{
  target                                 %<>%
  str_replace_all("\"", "&quot;&break;") %>%
  str_replace_all("'", "&apo;&break;")   %>%
  str_split("&break;")                   %>%
  unlist()

  safe_pieces    <- grep("(&quot;)|(&apo;)", target, invert = TRUE)
  contain_quotes <- grep("&quot;", target)
  contain_apo    <- grep("&apo;", target)

  if(length(safe_pieces) > 0) 
      target[safe_pieces] <- paste0("\"", target[safe_pieces], "\"")

  if(length(contain_quotes) > 0)
  {
    target[contain_quotes] <- paste0("'", target[contain_quotes], "'")
    target[contain_quotes] <- gsub("&quot;", "\"", target[contain_quotes])
  }

  if(length(contain_apo) > 0)
  {
    target[contain_apo] <- paste0("\"", target[contain_apo], "\"")
    target[contain_apo] <- gsub("&apo;", "'", target[contain_apo])
  }

  fragment <- paste0(target, collapse = ",")
  return(paste0("//*[contains(text(),concat(", fragment, "))]"))
}
Run Code Online (Sandbox Code Playgroud)

现在我们可以像这样生成一个有效的 xpath:

safe_xpath(target)
#> [1] "//*[contains(text(),concat('Fat\"',\"her'\",\"s son\"))]"
Run Code Online (Sandbox Code Playgroud)

以便

html %>% xml2::read_html() %>% html_nodes(xpath = safe_xpath(target))
#> {xml_nodeset (1)}
#> [1] <div>Fat"her's son</div>
Run Code Online (Sandbox Code Playgroud)