http-status-code-404 - Mechanize/Ruby读取404页面源代码

标签 http-status-code-404 mechanize

我所做的只是加载 Mechanize ,并获得一个返回 404 的页面。但这正是我想要的。 404 页面有很多我想在我的例子中使用的 html。

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

我似乎找不到关于此的任何进一步信息。

最佳答案

您需要处理异常:

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

关于http-status-code-404 - Mechanize/Ruby读取404页面源代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13188195/

相关文章:

python - 登录后 Mechanize 不会重定向到主网址

python, mechanize - 使用 mechanize 打开文本文件

perl - 解析时我没有得到 HTML 标签

Nginx - 将所有 404 错误传递回 PHP-FPM 以进行自定义错误页面处理

c# - HttpWebResponse 返回 404 错误

python - Flask 的内置服务器总是 404 并设置了 SERVER_NAME

python - 如何使用 python 和 Mechanize 模拟 AJAX 调用(XHR)

python-3.x - 使用 Python 进行网页抓取,需要登录才能查看输出

iis - WebResource.axd 404 用于我的 SiteMapPath CSS 和其他

python - Django Ckeditor 图像浏览器找不到图像