2017-05-14 139 views
1

我想從該網站mangafox一個圖片,圖片顯示在導航器,但我不斷收到錯誤在Web瀏覽器的工作原理鏈接白衣紅寶石紅寶石:無法打開

到目前爲止,我有嘗試這樣:

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
壞URI(是不是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

OpenURI :: HTTPError
403禁止

回答

2

使用OpenURI是在緊要關頭罰款,但你會用更強大的網絡圖書館像網絡可以更好地服務:: HTTP或百頭巨怪:

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 

(注:共享前測試了這 - 它加載罰款)

+0

它完美的作品非常感謝。 必須查看處理錯誤的文檔 –