0
 lotofxpath = arrayofmanyxpaths.map{|s| "\"" + s + "\""}.join(",")
 puts lotofxpath #=> "/html/body/a[1]", "/html/body/a[2]"

 newb = doc.xpath(lotofxpath).to_a

这将不起作用,并抱怨无效的 xpath。

但是,复制粘贴输出字符串

 newb = doc.xpath("/html/body/a[1]", "/html/body/a[2]").to_a

将毫无问题地工作!!!

这里发生了什么?

4

1 回答 1

1

在第一种情况下,您最终会按如下方式调用 Nokogiri

newb = doc.xpath("\"/html/body/a[1]\", \"/html/body/a[2]\"").to_a

这不是完成您想要做的事情的正确 Ruby 语法。正确的方法是

newb = doc.xpath(*arrayofmanyxpaths).to_a
于 2009-11-27T12:35:17.057 回答