我正在尝试通过Mechanize提交登录表单,但提交时没有任何操作。我再次获得一个空的登录表单。表单只有一个ID,没有Name,所以我使用索引来访问它。我并非100%自信我甚至填写了正确的表格,因此也欢迎直接使用id =“user-login”访问表单。谢谢!
这是我的代码:
require 'rubygems'
require 'mechanize'
require 'logger'
login_url = "http://www.peersnet.org/user/login"
agent = Mechanize.new{|a| a.log = Logger.new(STDERR) }
agent.user_agent_alias = 'Mac Safari'
page = agent.get(login_url)
form = page.forms[1]
form.name = username
form.pass = password
page = form.submit
puts page.body
和输出:
I, [2011-06-27T14:08:18.161313 #34326] INFO -- : Net::HTTP::Get: /user/login
D, [2011-06-27T14:08:18.161442 #34326] DEBUG -- : request-header: accept-language => en-us,en;q=0.5
D, [2011-06-27T14:08:18.161487 #34326] DEBUG -- : request-header: accept => */*
D, [2011-06-27T14:08:18.161540 #34326] DEBUG -- : request-header: user-agent => Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_2; de-at) AppleWebKit/531.21.8 (KHTML, like Gecko) Version/4.0.4 Safari/531.21.10
D, [2011-06-27T14:08:18.161574 #34326] DEBUG -- : request-header: connection => keep-alive
D, [2011-06-27T14:08:18.161606 #34326] DEBUG -- : request-header: accept-encoding => gzip,identity
D, [2011-06-27T14:08:18.161637 #34326] DEBUG -- : request-header: host => www.peersnet.org
D, [2011-06-27T14:08:18.161669 #34326] DEBUG -- : request-header: accept-charset => ISO-8859-1,utf-8;q=0.7,*;q=0.7
D, [2011-06-27T14:08:18.161707 #34326] DEBUG -- : request-header: keep-alive => 300
D, [2011-06-27T14:08:18.214246 #34326] DEBUG -- : Read 2280 bytes
D, [2011-06-27T14:08:18.214495 #34326] DEBUG -- : Read 3728 bytes
D, [2011-06-27T14:08:18.228916 #34326] DEBUG -- : Read 3946 bytes
D, [2011-06-27T14:08:18.229057 #34326] DEBUG -- : response-header: etag => "3eff27922ab4d458eadc2460edefc455"
D, [2011-06-27T14:08:18.229095 #34326] DEBUG -- : response-header: x-powered-by => PHP/5.2.6-1+lenny10
D, [2011-06-27T14:08:18.229130 #34326] DEBUG -- : response-header: expires => Sun, 19 Nov 1978 05:00:00 GMT
D, [2011-06-27T14:08:18.229178 #34326] DEBUG -- : response-header: last-modified => Mon, 27 Jun 2011 19:00:56 GMT
D, [2011-06-27T14:08:18.229213 #34326] DEBUG -- : response-header: content-type => text/html; charset=utf-8
D, [2011-06-27T14:08:18.229251 #34326] DEBUG -- : response-header: connection => Keep-Alive
D, [2011-06-27T14:08:18.229291 #34326] DEBUG -- : response-header: server => Apache/2.2.9 (Debian) PHP/5.2.6-1+lenny10 with Suhosin-Patch
D, [2011-06-27T14:08:18.229325 #34326] DEBUG -- : response-header: date => Mon, 27 Jun 2011 19:09:50 GMT
D, [2011-06-27T14:08:18.229365 #34326] DEBUG -- : response-header: content-length => 3946
D, [2011-06-27T14:08:18.229399 #34326] DEBUG -- : response-header: content-encoding => gzip
D, [2011-06-27T14:08:18.229433 #34326] DEBUG -- : response-header: set-cookie => SESS823dcdeff457c20dcc3729915d224656=6b452cc3217225dd1143317293459b49; expires=Wed, 20 Jul 2011 22:43:10 GMT; path=/; domain=.peersnet.org
D, [2011-06-27T14:08:18.229467 #34326] DEBUG -- : response-header: keep-alive => timeout=15, max=100
D, [2011-06-27T14:08:18.229501 #34326] DEBUG -- : response-header: cache-control => must-revalidate
D, [2011-06-27T14:08:18.229547 #34326] DEBUG -- : gunzip body
D, [2011-06-27T14:08:18.230961 #34326] DEBUG -- : saved cookie: SESS823dcdeff457c20dcc3729915d224656=6b452cc3217225dd1143317293459b49
I, [2011-06-27T14:08:18.231392 #34326] INFO -- : status: 200
D, [2011-06-27T14:08:18.234560 #34326] DEBUG -- : query: ANONYMIZEDINFOform_id=user_login"
I, [2011-06-27T14:08:18.236906 #34326] INFO -- : Net::HTTP::Post: /user/login
D, [2011-06-27T14:08:18.236960 #34326] DEBUG -- : request-header: accept-language => en-us,en;q=0.5
D, [2011-06-27T14:08:18.237012 #34326] DEBUG -- : request-header: accept => */*
D, [2011-06-27T14:08:18.237123 #34326] DEBUG -- : request-header: content-type => application/x-www-form-urlencoded
D, [2011-06-27T14:08:18.237170 #34326] DEBUG -- : request-header: user-agent => Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_2; de-at) AppleWebKit/531.21.8 (KHTML, like Gecko) Version/4.0.4 Safari/531.21.10
D, [2011-06-27T14:08:18.237209 #34326] DEBUG -- : request-header: connection => keep-alive
D, [2011-06-27T14:08:18.237252 #34326] DEBUG -- : request-header: accept-encoding => gzip,identity
D, [2011-06-27T14:08:18.237286 #34326] DEBUG -- : request-header: content-length => 98
D, [2011-06-27T14:08:18.237320 #34326] DEBUG -- : request-header: referer => http://www.peersnet.org/user/login
D, [2011-06-27T14:08:18.237353 #34326] DEBUG -- : request-header: cookie => SESS823dcdeff457c20dcc3729915d224656=6b452cc3217225dd1143317293459b49
D, [2011-06-27T14:08:18.237386 #34326] DEBUG -- : request-header: if-modified-since => Mon, 27 Jun 2011 19:00:56 GMT
D, [2011-06-27T14:08:18.237425 #34326] DEBUG -- : request-header: host => www.peersnet.org
D, [2011-06-27T14:08:18.237459 #34326] DEBUG -- : request-header: accept-charset => ISO-8859-1,utf-8;q=0.7,*;q=0.7
D, [2011-06-27T14:08:18.237493 #34326] DEBUG -- : request-header: keep-alive => 300
D, [2011-06-27T14:08:18.560518 #34326] DEBUG -- : Read 945 bytes
D, [2011-06-27T14:08:18.561055 #34326] DEBUG -- : Read 3969 bytes
D, [2011-06-27T14:08:18.561177 #34326] DEBUG -- : response-header: x-powered-by => PHP/5.2.6-1+lenny10
D, [2011-06-27T14:08:18.561234 #34326] DEBUG -- : response-header: last-modified => Mon, 27 Jun 2011 19:09:50 GMT
D, [2011-06-27T14:08:18.561288 #34326] DEBUG -- : response-header: expires => Sun, 19 Nov 1978 05:00:00 GMT
D, [2011-06-27T14:08:18.561341 #34326] DEBUG -- : response-header: content-type => text/html; charset=utf-8
D, [2011-06-27T14:08:18.561401 #34326] DEBUG -- : response-header: connection => Keep-Alive
D, [2011-06-27T14:08:18.561457 #34326] DEBUG -- : response-header: server => Apache/2.2.9 (Debian) PHP/5.2.6-1+lenny10 with Suhosin-Patch
D, [2011-06-27T14:08:18.561503 #34326] DEBUG -- : response-header: date => Mon, 27 Jun 2011 19:09:50 GMT
D, [2011-06-27T14:08:18.561557 #34326] DEBUG -- : response-header: content-length => 3969
D, [2011-06-27T14:08:18.561603 #34326] DEBUG -- : response-header: content-encoding => gzip
D, [2011-06-27T14:08:18.561648 #34326] DEBUG -- : response-header: keep-alive => timeout=15, max=99
D, [2011-06-27T14:08:18.561693 #34326] DEBUG -- : response-header: vary => Accept-Encoding
D, [2011-06-27T14:08:18.561738 #34326] DEBUG -- : response-header: cache-control => store, no-cache, must-revalidate, post-check=0, pre-check=0
D, [2011-06-27T14:08:18.561800 #34326] DEBUG -- : gunzip body
I, [2011-06-27T14:08:18.562356 #34326] INFO -- : status: 200
答案 0 :(得分:3)
只需使用Mechanize::Page::form_with(criteria)
方法
例如:
require 'rubygems'
require 'mechanize'
login_url = "http://www.peersnet.org/user/login"
agent = Mechanize.new
agent.user_agent_alias = 'Mac Safari'
page = agent.get(login_url)
form = page.form_with(:id => 'user-login')
form.name = username
form.pass = password
page = form.submit
puts page.body
由于缺少bugmenot帐户,我没有尝试过,但它应该有用......