Ruby open-uri重定向被禁止

时间:2014-12-10 18:08:34

标签: ruby open-uri

我有这个简单的html解析器(用于学习目的),我一直在努力。:

require 'open-uri'
puts "Enter URL to parse HTML: "
url = gets.chomp
puts "Enter tag to parse from: "
tag = gets.chomp
response = open(url).read
title1 = response.index(tag)
title2 = response.index(tag.insert(1,'/')) -1
result = response[(title1 + tag.length - 1)..title2]
print result 

当我输入http://twitter.com时,收到此错误消息:

ERROR: `open_loop': redirection forbidden: http://twitter.com -> https://twitter.com/ (RuntimeError)
from /usr/local/rvm/rubies/ruby-2.1.4/lib/ruby/2.1.0/open-uri.rb:149:in `open_uri'
from /usr/local/rvm/rubies/ruby-2.1.4/lib/ruby/2.1.0/open-uri.rb:704:in `open'
from /usr/local/rvm/rubies/ruby-2.1.4/lib/ruby/2.1.0/open-uri.rb:34:in `open'
from /home/ubuntu/workspace/htmlparse.rb:6:in `<main>' 

有任何建议或帮助吗?我是Ruby新手,我知道其他html解析模块,但我这样做是为了学习Ruby基础知识。感谢。

3 个答案:

答案 0 :(得分:29)

看看open_uri_redirections宝石。

它修补了Ruby的OpenURI,允许从HTTP重定向到HTTPS,反之亦然。

答案 1 :(得分:15)

您还可以捕获异常,然后使用“https”网址重新尝试。

url = "http://classic.ona.io/api/v1/files/3538545?filename=gringgo/attachments/1485229166168.jpg"

uri = URI.parse(url)
tries = 3

begin
  uri.open(redirect: false)
rescue OpenURI::HTTPRedirect => redirect
  uri = redirect.uri # assigned from the "Location" response header
  retry if (tries -= 1) > 0
  raise
end

来源:https://twin.github.io/improving-open-uri/

答案 2 :(得分:4)

Ruby open-uri中的Ruby 2.4固定升级重定向(来自http - &gt; https),现在:

RUBY_VERSION
=> "2.4.2"

require 'open-uri'
=> true

open('http://twitter.com')
=> #<Tempfile:/tmp/open-uri20170926-24254-1kflwxq>

来源:http://blog.bigbinary.com/2017/03/02/open-uri-in-ruby-2-4-allows-http-to-https-redirection.html