ruby打开URL并进行救援

时间:2009-07-02 10:14:34

标签: ruby-on-rails ruby

我想查看旧网站上是否存在几个网址,并收集返回404的网址。

@paintings = Painting.find(:all)
@paintings.each do |painting|
  open("http://www.mydomain.com/" + painting.user.username.downcase + "/" + painting.permalink)
   rescue OpenURI::HTTPError
     @failure += painting.permalink
   else
     @success += painting.permalink
   end
  end

嗯,我无法通过救援方法来获取错误

syntax error, unexpected kRESCUE, expecting kEND
      rescue OpenURI::HTTPError

有什么想法吗?

1 个答案:

答案 0 :(得分:20)

您似乎在开放之前忘记了begin

 begin
  open("http://www.mydomain.com/" + painting.user.username.downcase + "/" + painting.permalink)
 rescue OpenURI::HTTPError
   @failure += painting.permalink
 else
   @success += painting.permalink
 end