不确定为什么在浏览器中URI可以正常工作,但是我在这里却收到了以下错误:
http://oracleofbacon.org/cgi-bin/xml?a=Kevin Bacon&b=Tom Cruise&u=1&p=google-apps
这是我的代码:
def kb(to)
uri = "http://oracleofbacon.org/cgi-bin/xml?a=Kevin Bacon&b=#{to.strip}&u=1&p=google-apps"
doc = Nokogiri::XML(open(uri)) # throws error on this line
return parse(doc)
end
I get the following error:
in `split': bad URI(is not URI?): http://oracleofbacon.org/cgi-bin/xml?a=Kevin Bacon&b=Tom Cruise&u=1&p=google-apps (URI::InvalidURIError)`
我会以以下方式执行该方法:
kb("Tom Cruise")
#{to.strip.gsub(" ", "%20")}
- fulviorequire 'cgi'; CGI::escape("'Stop!' said Fred") # => "%27Stop%21%27+said+Fred"
(doc). (或者在Rails中使用h
辅助方法)。 - steenslaggsub
是可以的。除此之外,您应该利用预先发明的轮子并选择URI或Addressable gem,它非常全面。 - the Tin Man