Ruby Open-URI重定向禁止 [英] Ruby open-uri redirect forbidden

查看:67
本文介绍了Ruby Open-URI重定向禁止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在使用的简单的html解析器(出于学习目的).

I have this simple html parser(for learning purposes) that I have been working on.:

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时,出现以下错误消息:

and when I input http://twitter.com, I get this error message:

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基础知识.谢谢.

Any advise or help? Im new to Ruby and I am aware of other html parsing modules, but Im doing this to learn Ruby basics. Thanks.

推荐答案

看看 open_uri_redirections 宝石.

它修补了Ruby的OpenURI,以允许从HTTP重定向到HTTPS或以其他方式进行重定向.

It patches Ruby's OpenURI to allow redirections from HTTP to HTTPS or the other way around.

这篇关于Ruby Open-URI重定向禁止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆