0

Consider the following example of web scraping using R.

m <- "https://stackoverflow.com"
doc <- htmlParse(GET(m))

hello <- length(xpathSApply(doc, "//a/@href[contains(.,'stackoverflow')]"))
hello
[1] 48

The XPath function I use gives me the number of all links that contain the string "stackoverflow".

My goal is to replace the string "stackoverflow" in the XPath function with a variable. Like this:

my_variable <- "stackoverflow"
hello <- length(xpathSApply(doc, "//a/@href[contains(.,my_variable)]"))

It doesn't work for now cause the data variable is considered a string. Can you please help me to explicit my_variable is a variable?

Matheus Lacerda
  • 5,983
  • 11
  • 29
  • 45
Remi
  • 961
  • 1
  • 13
  • 25

1 Answers1

0

The answer has been given by Parfait : Use paste for the xpath:

paste0("//a/@href[contains(.,'", my_variable,"')]")
Remi
  • 961
  • 1
  • 13
  • 25