【发布时间】:2012-04-22 10:47:24
【问题描述】:
我在我的 Ruby on Rails 应用程序中使用 Mechanize 和 Nokogiri 来抓取我们的本地打印机管理面板,以检索打印机生命周期内的打印页数。
我有以下 rake 任务:
# Logs into printer admin page and retrieved counts.
require 'rubygems'
require 'mechanize'
require 'logger'
# Create a new mechanize object
agent = Mechanize.new
# Load the printer admin page
page = agent.get("http://192.168.1.126/index.html?lang=1")
# Select the form with an action of index.cqi
form = agent.page.form_with(:action => "index.cgi")
form.radiobuttons_with(:id => '0x3fdb24153404')[1]
# Submit the form
page = form.submit form.buttons.first
pp page
返回以下内容:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<script type="text/javascript">
<!--
window.onload=function(){setTimeout(function(){document.menu_link.submit();},0);}
//-->
</script>
</head>
<body>
<form name="menu_link" action="index.html" method="post" enctype="application/x-www-form-urlencoded">
<input type="hidden" name="lang" value="1">
</form>
</body>
</html>
我似乎无法选择上述页面上的表单,并且脚本似乎停在该页面并且没有遵循重定向。
是否有处理此类重定向的标准方法?也许暂停脚本直到重定向发生?它会允许重定向工作吗?
任何指针将不胜感激!
【问题讨论】:
-
第二页的表格怎么选?
-
我刚刚选择了名为 menu_link 的表单。
标签: javascript ruby-on-rails-3 redirect nokogiri mechanize