我试图让ruby逃避/绕过404错误,但它似乎不工作。下面是我到目前为止的代码:
require 'rubygems'
require 'simple_oauth'
require 'cloudsight'
require 'rubygems'
require 'net/http'
require 'uri'
require 'json'
require 'open-uri'
require 'openssl'
require 'hpricot'
require 'nokogiri'
#T FILE TO JPG
webby = 'https://t.co/5h92pCmoPM'
OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE
doc = Hpricot(open(webby)).to_s
jpgstart = '<meta property="og:image" content="'
jpgstop = '" />'
jpglink = doc[/#{jpgstart}(.*?)#{jpgstop}/m, 1]
print jpglink
这是我得到的错误:
C:/Users/User/Desktop/test89:18: warning: already initialized constant OpenSSL::SSL::VERIFY_PEER
C:/Ruby21/lib/ruby/2.1.0/open-uri.rb:353:in `open_http': 404 NOT FOUND (OpenURI::HTTPError)
from C:/Ruby21/lib/ruby/2.1.0/open-uri.rb:724:in `buffer_open'
from C:/Ruby21/lib/ruby/2.1.0/open-uri.rb:210:in `block in open_loop'
from C:/Ruby21/lib/ruby/2.1.0/open-uri.rb:208:in `catch'
from C:/Ruby21/lib/ruby/2.1.0/open-uri.rb:208:in `open_loop'
from C:/Ruby21/lib/ruby/2.1.0/open-uri.rb:149:in `open_uri'
from C:/Ruby21/lib/ruby/2.1.0/open-uri.rb:704:in `open'
from C:/Ruby21/lib/ruby/2.1.0/open-uri.rb:34:in `open'
from C:/Users/KVadher/Desktop/test89:20:in `<main>'
[Finished in 2.7s with exit code 1]
有没有人知道如何绕过这个错误?
这是你想要的吗?
require 'rubygems'
require 'simple_oauth'
require 'cloudsight'
require 'rubygems'
require 'net/http'
require 'uri'
require 'json'
require 'open-uri'
require 'openssl'
require 'hpricot'
require 'nokogiri'
#T FILE TO JPG
webby = 'https://t.co/5h92pCmoPM'
OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE
begin
doc = Hpricot(open(webby)).to_s
jpgstart = '<meta property="og:image" content="'
jpgstop = '" />'
jpglink = doc[/#{jpgstart}(.*?)#{jpgstop}/m, 1]
print jpglink
rescue OpenURI::HTTPError
# do nothing. bypass error.
end