我有这个简单的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
Run Code Online (Sandbox Code Playgroud)
当我输入时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>'
Run Code Online (Sandbox Code Playgroud)
有任何建议或帮助吗?我是Ruby新手,我知道其他html解析模块,但我这样做是为了学习Ruby基础知识.谢谢.
小智 16
您还可以捕获异常,然后使用"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
Run Code Online (Sandbox Code Playgroud)
资料来源:https://twin.github.io/improving-open-uri/
Ruby 2.4 修复了升级重定向(从 http -> https)open-uri,现在:
RUBY_VERSION
=> "2.4.2"
require 'open-uri'
=> true
open('http://twitter.com')
=> #<Tempfile:/tmp/open-uri20170926-24254-1kflwxq>
Run Code Online (Sandbox Code Playgroud)
来源:http : //blog.bigbinary.com/2017/03/02/open-uri-in-ruby-2-4-allows-http-to-https-redirection.html