Ruby / Rails - 错误的URI

时间:2012-01-03 08:17:21

标签: ruby-on-rails ruby uri nokogiri

当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

我收到以下错误:

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")

1 个答案:

答案 0 :(得分:20)

这是因为浏览器在路径上是友好的,就像小狗一样,并且会不遗余力地渲染页面或解析URL。申请不会这样做,因为你必须告诉它如何友好。

您的网址无效,因为它有嵌入空格。用%20替换空格:

irb -f
irb(main):001:0> require 'open-uri'
=> true
irb(main):002:0> open('http://oracleofbacon.org/cgi-bin/xml?a=Kevin%20Bacon&b=Tom%20Cruise&u=1&p=google-apps').read
=> "<?xml version=\"1.0\" standalone=\"no\"?>\n<link><actor>Tom Cruise</actor><movie>A Few Good Men (1992)</movie><actor>Kevin Bacon</actor></link>"

转义需要转义的字符非常简单:

irb -f
irb(main):001:0> require 'uri'
=> true
irb(main):002:0> URI.escape('http://oracleofbacon.org/cgi-bin/xml?a=Kevin Bacon&b=Tom Cruise&u=1&p=google-apps')
=> "http://oracleofbacon.org/cgi-bin/xml?a=Kevin%20Bacon&b=Tom%20Cruise&u=1&p=google-apps"