views:

21

answers:

1

Hi,

I'm trying to parse web pages but I sometimes get 404 errors. Here's the code I use to get the web page:

result = Net::HTTP::get URI.parse(URI.escape(url))

How do I test if result is a 404 error code?

Thank you,

Kevin

+1  A: 

Rewrite your code like this:

uri = URI.parse(url)
result = Net::HTTP.start(uri.host, uri.port) { |http| http.get(uri.path) }
puts result.code
puts result.body

That will print the status code followed by the body.

Theo
Thanks! I tried this but result.status didn't work. Instead, I checked result.class.name == "HTTPOK", it does the trick :-)
Kevin
Sorry, it should have been `result.code`, not `result.status`. I've updated my answer.
Theo