Ruby - 获取页面内容,即使它不存在
Ruby - Getting page content even if it doesn't exist
我正在尝试将一系列自定义 404 页面放在一起。
require 'uri'
def open(url)
page_content = Net::HTTP.get(URI.parse(url))
puts page_content.content
end
open('http://somesite.com/1ygjah1761')
以下代码退出程序时出现错误。如何从网站获取页面内容,无论是否为 404。
Net::HTTP.get
returns 页面内容直接作为字符串,所以不需要在结果上调用.content
:
page_content = Net::HTTP.get(URI.parse(url))
puts page_content
你需要从错误中解救出来
def open(url)
require 'net/http'
page_content = ""
begin
page_content = Net::HTTP.get(URI.parse(url))
puts page_content
rescue Net::HTTPNotFound
puts "THIS IS 404" + page_content
end
end
您可以在此处找到有关此类内容的更多信息:http://tammersaleh.com/posts/rescuing-net-http-exceptions/
我正在尝试将一系列自定义 404 页面放在一起。
require 'uri'
def open(url)
page_content = Net::HTTP.get(URI.parse(url))
puts page_content.content
end
open('http://somesite.com/1ygjah1761')
以下代码退出程序时出现错误。如何从网站获取页面内容,无论是否为 404。
Net::HTTP.get
returns 页面内容直接作为字符串,所以不需要在结果上调用.content
:
page_content = Net::HTTP.get(URI.parse(url))
puts page_content
你需要从错误中解救出来
def open(url)
require 'net/http'
page_content = ""
begin
page_content = Net::HTTP.get(URI.parse(url))
puts page_content
rescue Net::HTTPNotFound
puts "THIS IS 404" + page_content
end
end
您可以在此处找到有关此类内容的更多信息:http://tammersaleh.com/posts/rescuing-net-http-exceptions/