1

I m using R and the xpathSApply function to get elements from a web page that contains specific strings. I m trying to use the contains function from xpath with several elements I stored in a list called my_list.

my_list <- c("word1", "word2", "word3")
xpathSApply(doc, "//h2[contains(text(),'paste(my_list, collapse='|')')]")

For now it doesn't work, so I divided it into several lines:

xpathSApply(doc, "//h2[contains(text(),'word1')]")
xpathSApply(doc, "//h2[contains(text(),'word2')]")
xpathSApply(doc, "//h2[contains(text(),'word3')]")

Can you please help me?

Remi
  • 961
  • 1
  • 13
  • 25

1 Answers1

1

If you want to match node that contains one of substrings you can try

"//h2[contains(text(),'word1') or contains(text(),'word2') or contains(text(),'word3')]"
Andersson
  • 51,635
  • 17
  • 77
  • 129