3

我正在尝试从我的公司 Intranet 中抓取信息,以便我可以通过仪表板在我们的办公室墙板上显示信息。我正在尝试使用以下提供的信息:此站点。除了菜鸟之外,我遇到的问题是,为了访问我想要抓取的信息,我需要登录到我们的 Intranet 提供我在一个页面上的用户名然后提交到另一个页面,以便我可以提供我的密码。登录后,我可以链接和抓取我的数据。

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

<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>

这是我的登录密码页面的一些来源:

<form action='loginauthprocess.asp?UserName=******&Page=&PassedURL=' target='_top' method='post' onsubmit='checkMyBrowser();' 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;'>Password:</span><br><input class='normal' type='password' autocomplete='off' id='LoginPassword' name='LoginPassword' style='border: 1px solid gray; height: 16px; font-family: arial; font-size: 11; width: 180px;' maxlength='30'><input class='normal_button' type='submit' value='Log In' style='border: 1px solid gray; font-weight: bold; width: 80px; margin-left: 10px;' onclick="var password=document.getElementById('LoginPassword').value; if (password.length > 2) { submit(); } else { alert('Enter your Password.'); }"></form>

使用所述资源的示例,这是我认为应该有效但似乎不是:

require 'mechanize'
@agent = Mechanize.new
@agent.verify_mode = OpenSSL::SSL::VERIFY_NONE

##Login Page:
page = @agent.get 'http://www.website_here.com/intranet/login.asp'

##Username Page:
form = page.forms[0]
form['USER NAME HERE'] = LoginUser
##Submit User:
page = form.submit

##Password Page:
form = page.forms[0]
form['USER PASSWORD HERE'] = LoginPassword
##Submit Password:
page = form.submit

当我测试我的代码时,我得到以下输出:

test.rb:10:in `': 未初始化的常量 LoginUser (NameError)

谁能指出我做错了什么?

谢谢

编辑 3/27/15:

使用@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>': undefined local variable or methodagent' for main:Object (NameError)

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

编辑 4/4/15:

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

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_missing': undefined methodloginUser=' for # (NoMethodError) from scraper.rb:10:in `'

我错过了什么或输入错误?请参阅我的第一次编辑以了解我的表单是如何编码的。另外要清楚的是,我没有以这种方式对表格进行编码。我只是想学习如何编码和抓取需要在我的 Dashing Dashboard 项目上显示的数据。

4

3 回答 3

3

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

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

我从 user:Senthess HERE找到了以下示例。我仍然不是 100% 了解所有单独的代码在做什么,所以如果有人想花时间分解它,请这样做。这将有助于我自己和其他人更好地理解。

谢谢!

于 2015-04-06T17:51:57.103 回答
1

我刚刚查找了 Mechanize gem 并找到了相关的解决方案。您必须在输入字段上设置正确的“名称”。否则你不能接受他们的价值。关注这篇文章。

http://crabonature.pl/posts/23-automation-with-mechanize-and-ruby

于 2015-03-27T11:57:17.313 回答
0

不确定您是否找到了这些,但 Mechanize 有相当出色的文档:http ://docs.seattlerb.org/mechanize/GUIDE_rdoc.html

从这些中,我在irb REPL 中玩来创建这个登录到 GitHub的简单爬虫: https ://gist.github.com/tylermauthe/781f68add24819e207c4

于 2015-04-03T19:21:48.870 回答