机械化/Ruby读取404页面的源代码 [英] Mechanize/Ruby read source code of 404 page

查看:73
本文介绍了机械化/Ruby读取404页面的源代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所要做的就是加载机械化,并获得一个返回404的页面.但这正是我想要的. 404页面上有很多我想在示例中使用的html.

All I'm doing is loading mechanize, and getting a page that returns 404. But that's exactly what I want. The 404 page has plenty of html I'd like to use in my example.

a = mechanize.new
a.get('http://www.youtube.com/watch?v=e4g8jriw4rg')
a.page
=> nil

我似乎找不到任何进一步的信息.

I can't seem to find any further info on this.

推荐答案

您需要处理异常:

begin
  page = a.get 'http://www.youtube.com/watch?v=e4g8jriw4rg'
rescue Mechanize::ResponseCodeError => e
  puts e.response_code # the status code as a string, e.g. "404"
  page = e.page
end

puts page.title

这篇关于机械化/Ruby读取404页面的源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆