Ruby:无法打开可在网络浏览器中运行的 link
Ruby : cannot open a link that works in a web browser
我正在尝试从网站 mangafox 获取图片,图片显示在导航器中,但我一直收到错误 Ruby
到目前为止,我已经试过了:
require 'open-uri'
require 'pp'
def get_page(link)
page = nil
begin
page = open(link, 'User-Agent' => "Ruby/#{RUBY_VERSION}")
rescue Exception => e
puts e.class.to_s
puts e.message
end
return page
end
link = 'http://h.mfcdn.net/store/manga/9/14-116.0/compressed/Bleach-14-116[manga-rain]._manga_rain_bleach_ch116_01.jpg?token=24530ad3411b28ed7f5ef17f932e8713&ttl=1494853200'
# tried this after researching on internet because some characters are refused in links ( such as '[' or ']' )
link2 = link.gsub(/[\[\]]/) { '%%%s' % $&.ord.to_s(16) }.chomp
pp get_page(link)
pp get_page(link2)
但我得到了这个输出:
URI::InvalidURIError
bad URI(is not URI?): http://h.mfcdn.net/store/manga/9/14-116.0/compressed/Bleach-14-116[manga-rain]._manga_rain_bleach_ch116_01.jpg?token=24530ad3411b28ed7f5ef17f932e8713&ttl=1494853200
nil
OpenURI::HTTPError
403 Forbidden
nil
在紧要关头使用 OpenURI 很好,但您最好使用更强大的网络库,例如 Net::HTTP 或 Typhoeus:
response = Typhoeus.get('http://h.mfcdn.net/store/manga/9/14-116.0/compressed/Bleach-14-116[manga-rain]._manga_rain_bleach_ch116_01.jpg?token=24530ad3411b28ed7f5ef17f932e8713&ttl=1494853200')
response.body #=> binary image data
(注意:在分享之前对此进行了测试——加载正常)
我正在尝试从网站 mangafox 获取图片,图片显示在导航器中,但我一直收到错误 Ruby
到目前为止,我已经试过了:
require 'open-uri'
require 'pp'
def get_page(link)
page = nil
begin
page = open(link, 'User-Agent' => "Ruby/#{RUBY_VERSION}")
rescue Exception => e
puts e.class.to_s
puts e.message
end
return page
end
link = 'http://h.mfcdn.net/store/manga/9/14-116.0/compressed/Bleach-14-116[manga-rain]._manga_rain_bleach_ch116_01.jpg?token=24530ad3411b28ed7f5ef17f932e8713&ttl=1494853200'
# tried this after researching on internet because some characters are refused in links ( such as '[' or ']' )
link2 = link.gsub(/[\[\]]/) { '%%%s' % $&.ord.to_s(16) }.chomp
pp get_page(link)
pp get_page(link2)
但我得到了这个输出:
URI::InvalidURIError
bad URI(is not URI?): http://h.mfcdn.net/store/manga/9/14-116.0/compressed/Bleach-14-116[manga-rain]._manga_rain_bleach_ch116_01.jpg?token=24530ad3411b28ed7f5ef17f932e8713&ttl=1494853200
nil
OpenURI::HTTPError
403 Forbidden
nil
在紧要关头使用 OpenURI 很好,但您最好使用更强大的网络库,例如 Net::HTTP 或 Typhoeus:
response = Typhoeus.get('http://h.mfcdn.net/store/manga/9/14-116.0/compressed/Bleach-14-116[manga-rain]._manga_rain_bleach_ch116_01.jpg?token=24530ad3411b28ed7f5ef17f932e8713&ttl=1494853200')
response.body #=> binary image data
(注意:在分享之前对此进行了测试——加载正常)