Ruby 在两个单独的页面上删除需要登录用户名和密码的站点

Ruby 在两个单独的页面上删除需要登录用户名和密码的站点,ruby,widget,web-scraping,mechanize,dashing,Ruby,Widget,Web Scraping,Mechanize,Dashing,我正试图从我公司的内部网中获取信息,这样我就可以通过仪表板在办公室的墙上显示信息。我正在尝试使用以下提供的信息:。除了作为noob之外,我遇到的问题是,为了访问我想要获取的信息,我需要登录到我们的内部网,在一个页面上提供我的用户名,然后提交到另一个页面,以便我可以提供密码。一旦我登录,我就可以链接和刮取我的数据 以下是我的登录用户名页面中的一些源代码: <form action='loginauthpwd.asp?PassedURL=' method='post' style='margi

我正试图从我公司的内部网中获取信息,这样我就可以通过仪表板在办公室的墙上显示信息。我正在尝试使用以下提供的信息:。除了作为noob之外,我遇到的问题是,为了访问我想要获取的信息,我需要登录到我们的内部网,在一个页面上提供我的用户名,然后提交到另一个页面,以便我可以提供密码。一旦我登录,我就可以链接和刮取我的数据

以下是我的登录用户名页面中的一些源代码:

<form action='loginauthpwd.asp?PassedURL=' method='post' style='margin: 0px;'><table border='0' cellspacing='1' width='999' height='350'><tr><td width='100'>&nbsp;</td><td valign='center' width='100'><table style='width: 350px; background-color: #EEEEEE; border: 1px solid gray;'><tr><td class='fontBlack' style='padding: 10px; vertical-align: top;'><span style='font-weight: bold;'>Username:</span><br><input type='text' class='normal' autocomplete='off' id='LoginUser' name='LoginUser' style='border: 1px solid gray; height: 16px; font-family: arial; font-size: 11; width: 180px;' maxlength='30'><input class='normal_button' type='button' value='Go' style='border: 1px solid gray; font-weight: bold; width: 80px; margin-left: 10px;' onclick="var username=document.getElementById('LoginUser').value; if (username.length > 2) { submit(); } else { alert('Enter your Username.'); }"></form>
当我测试代码时,我得到以下输出:

test.rb:10:in`:未初始化的常量登录用户(NameError)

有人能指出我做错了什么吗

谢谢

编辑2015年3月27日:

使用@seoyoochan资源,我试图形成如下代码:

require 'rubygems'
require 'mechanize'
login_page  = agent.get "http://www.website_here.com/intranet/loginauthusr.asp?Page="
login_form = login_page.form_with(action: '/sessions') 
user_field = login_form.field_with(name: "session[user]") 
user.value = 'My User Name'

login_form.submit
当我尝试运行代码时,我现在得到以下输出:

test.rb:4:in
':未定义的局部变量或方法
main:Object的代理(namererror)

我需要一个关于如何分配我提供的表单将使用的正确名称/类的示例

编辑4/4/15:

好的,现在使用@TylerAuthe示例,我尝试测试以下代码:

require 'mechanize'
require 'io/console'

agent = Mechanize.new
page = agent.get('http://www.website_here.com/intranet/loginauthusr.asp?Page=')

form = page.forms.find{|form| form.action.include?("loginauthpwd.asp?PassedURL=")}

puts "Login:"
form.login = gets.chomp
page = agent.submit(form)
pp page
现在我的想法是,这段代码应该允许我输入并提交我的用户名,让我进入下一个需要密码的页面。但是,当我尝试运行它并输入用户名时,会得到以下输出:

/var/lib/gems/1.9.1/gems/mechanize-2.7.3/lib/mechanize/form.rb:217:in
method_缺失:未定义的方法
logiuser='for#(命名错误) 从scraper.rb:10:in`


我遗漏了什么或输入了错误?请参阅我的第一次编辑,了解我的表单是如何编码的。同样要明确的是,我没有以这种方式对表单进行编码。我只是想学习如何编码和刮取显示在我的仪表板项目上所需的数据。

我刚刚查阅了Mechanize gem并找到了相关的解决方案。 必须在输入字段上设置正确的“名称”。否则,您无法接受来自它们的值。 跟随这篇文章


不确定您是否找到了这些文档,但Mechanize有相当出色的文档:

通过这些,我在REPL中创建了这个简单的scraper,它可以登录到 :

我可以使用以下示例登录。感谢所有帮助我学习所有资源和示例的人

require 'nokogiri'
require 'mechanize'

agent = Mechanize.new

# Below opens URL requesting username and finds first field and fills in form then submits page.

login = agent.get('http://www.website_here.com')
login_form = login.forms.first
username_field = login_form.field_with(:name => "user_session[username]")
username_field = "YOUR USERNAME HERE"
page = agent.submit login_form

# Below opens URL requesting password and finds first field and fills in form then submits page.

login = agent.get('http://www.website_here.com')
login_form = login.forms.first
password_field = login_form.field_with(:name => "user_session[password]")
password_field = "YOUR PASSWORD HERE"
page = agent.submit login_form

# Below will print page showing information confirming that you have logged in.

pp page
我从用户那里找到了以下示例:Senthess。我仍然不是100%了解所有的代码都在做什么,如果有人想花时间把它分解,请这样做。这将有助于我和其他人更好地理解


谢谢

谢谢,我今天到办公室后会看一看。是的,告诉我进展如何。我在表格上看到了一些潜在的安全缺陷。不要将javascript代码直接放在表单上。通过正确地实现令牌身份验证系统来使用令牌身份验证系统。好吧,我对这一点太陌生了,无法理解在给定的示例中需要匹配什么才能使其正常工作。我一直在玩一些游戏,似乎无法让我的第一个用户名页面在表单中提供我的姓名,并提交到下一个页面,该页面将询问我的密码。如果这有助于提供一个示例,我能够了解到我可以在Mechanize中创建一个“pp页面”,该页面将提供以下输出:{forms}这告诉我,我正在尝试访问的表单没有名称,对吗?是否导致了相同的错误?其余的日志是什么?您已经发布了两个实现,都有不同的问题。您更喜欢使用哪种实现?我的建议是@seoyoochan建议的解决方案。@tylerauthe老实说,我不在乎,只要它登录并刮取数据,并允许我在Dashing Dashboard中的小部件上显示输出。回看我上面的评论后,我可以看到它被断章取义。要明确的是…我对编码还不熟悉,我想要一个很好的例子,可以让我通过脚本/代码自动登录,这样我就可以尝试让它显示在我的小部件上。谢谢你的帮助!谢谢,我会检查一下,让你知道结果如何。好的,我在我的环境中使用了你的例子,并且能够登录到Git。话虽如此,Git在同一页面上有User和Password字段。就我而言;我需要能够首先提供用户名/提交页面,然后再次提供密码/提交页面。在这一点上,我将登录到我的内联网,以便我可以刮我的数据。我相信你用“形式.行动”的例子让我更接近了一步。我的表格也是零。所以现在的诀窍是能够浏览用户名和密码页面。我想知道为什么当你浏览pp页面时,密码字段不在哈希中…你应该能够根据我提供的要点来解决这个问题。您需要重新排列代码,这样您就不会在开始时尝试填写密码字段,这样您就可以在第二页的表单上填写密码,然后提交第二张表单……请参阅上面的最新编辑。感谢到目前为止所有的帮助和可能显示我仍然做错了什么的指针。
require 'mechanize'
require 'io/console'

agent = Mechanize.new
page = agent.get('http://www.website_here.com/intranet/loginauthusr.asp?Page=')

form = page.forms.find{|form| form.action.include?("loginauthpwd.asp?PassedURL=")}

puts "Login:"
form.login = gets.chomp
page = agent.submit(form)
pp page
require 'nokogiri'
require 'mechanize'

agent = Mechanize.new

# Below opens URL requesting username and finds first field and fills in form then submits page.

login = agent.get('http://www.website_here.com')
login_form = login.forms.first
username_field = login_form.field_with(:name => "user_session[username]")
username_field = "YOUR USERNAME HERE"
page = agent.submit login_form

# Below opens URL requesting password and finds first field and fills in form then submits page.

login = agent.get('http://www.website_here.com')
login_form = login.forms.first
password_field = login_form.field_with(:name => "user_session[password]")
password_field = "YOUR PASSWORD HERE"
page = agent.submit login_form

# Below will print page showing information confirming that you have logged in.

pp page