Firefox 和 Ruby Mechanize 之间的表单参数差异

发布于 2024-08-20 02:25:49 字数 2583 浏览 3 评论 0原文

我试图弄清楚 mechanize 是否发送正确的帖子查询

我想登录论坛(请参阅 html source, mechanize log 在我的其他问题中),但我再次只得到登录页面。在查看它时,我可以看到 Firefox 发送带有

auth_username=myusername&auth_password=mypassword&auth_login=Login 等参数的帖子,但我的脚本发送

auth_username=radek&auth_password=mypassword 可以吗?或者 &auth_login=Login 部分必须存在?

当我尝试使用 login_form['auth_login'] = 'Login' 添加它时,出现错误 gems/mechanize-0.9.3/lib/www/mechanize/page.rb:13 inmeta': undefined method search' for nil:NilClass (NoMethodError)

在我看来,auth_login是一个表单按钮而不是一个字段(我不知道这是否重要)

[#<WWW::Mechanize::Form
 {name nil}
 {method "POST"}
 {action
  "http://www.somedomain.com/login?auth_successurl=http://www.somedomain.com/forum/yota?baz_r=1"}
 {fields
  #<WWW::Mechanize::Form::Field:0x36946c0 @name="auth_username", @value="">
  #<WWW::Mechanize::Form::Field:0x369451c @name="auth_password", @value="">}
 {radiobuttons}
 {checkboxes}
 {file_uploads}
 {buttons
  #<WWW::Mechanize::Form::Button:0x36943b4
   @name="auth_login",
   @value="Login">}>
]

我的脚本如下

  require 'rubygems'
  require 'mechanize'
    require 'logger'

  agent = WWW::Mechanize.new {|a| a.log = Logger.new("loginYOTA.log") }

agent.follow_meta_refresh = true #Mechanize does not follow meta refreshes by default, we need to set that option.

page = agent.get("http://www.somedomain.com/login?auth_successurl=http://www.somedomain.com/forum/yota?baz_r=1")

login_form = page.form_with(:method => 'POST')  #works

puts login_form.buttons.inspect
puts page.forms.inspect
STDIN.gets

  login_form.fields.each { |f| puts "#{f.name} : #{f.value}" }  
#STDIN.gets
login_form['auth_username'] = 'myusername'
login_form['auth_password'] = 'mypassword'
  login_form['auth_login'] = 'Login'
STDIN.gets  
page = agent.submit login_form
#Display message if logged in

puts page.parser.xpath("/html/body/div/div/div/table/tr/td[2]/div/strong").xpath('text()').to_s.strip
  puts
puts page.parser.xpath("/html/body/div/div/div/table/tr/td[2]/div").xpath('text()').to_s.strip

output = File.open("login.html", "w") {|f| f.write(page.parser.to_html) }

您可以找到更多代码,html,登录我的其他相关问题 使用浏览器登录然后 ruby​​/mechanize 接管它?

I am trying to figure out if mechanize sends correct post query.

I want to log in to a forum (please see html source, mechanize log in my other question) but I get only the login page again. When looking into it I can see that firefox sends out post with parameters like

auth_username=myusername&auth_password=mypassword&auth_login=Login but my script sends

auth_username=radek&auth_password=mypassword is that ok or the &auth_login=Login part must be present?

When I tried to add it using login_form['auth_login'] = 'Login' I got an error gems/mechanize-0.9.3/lib/www/mechanize/page.rb:13 inmeta': undefined method search' for nil:NilClass (NoMethodError)

It seems to me that auth_login is a form button not a field (I don't know if it matters)

[#<WWW::Mechanize::Form
 {name nil}
 {method "POST"}
 {action
  "http://www.somedomain.com/login?auth_successurl=http://www.somedomain.com/forum/yota?baz_r=1"}
 {fields
  #<WWW::Mechanize::Form::Field:0x36946c0 @name="auth_username", @value="">
  #<WWW::Mechanize::Form::Field:0x369451c @name="auth_password", @value="">}
 {radiobuttons}
 {checkboxes}
 {file_uploads}
 {buttons
  #<WWW::Mechanize::Form::Button:0x36943b4
   @name="auth_login",
   @value="Login">}>
]

My script is as follow

  require 'rubygems'
  require 'mechanize'
    require 'logger'

  agent = WWW::Mechanize.new {|a| a.log = Logger.new("loginYOTA.log") }

agent.follow_meta_refresh = true #Mechanize does not follow meta refreshes by default, we need to set that option.

page = agent.get("http://www.somedomain.com/login?auth_successurl=http://www.somedomain.com/forum/yota?baz_r=1")

login_form = page.form_with(:method => 'POST')  #works

puts login_form.buttons.inspect
puts page.forms.inspect
STDIN.gets

  login_form.fields.each { |f| puts "#{f.name} : #{f.value}" }  
#STDIN.gets
login_form['auth_username'] = 'myusername'
login_form['auth_password'] = 'mypassword'
  login_form['auth_login'] = 'Login'
STDIN.gets  
page = agent.submit login_form
#Display message if logged in

puts page.parser.xpath("/html/body/div/div/div/table/tr/td[2]/div/strong").xpath('text()').to_s.strip
  puts
puts page.parser.xpath("/html/body/div/div/div/table/tr/td[2]/div").xpath('text()').to_s.strip

output = File.open("login.html", "w") {|f| f.write(page.parser.to_html) }

You can find more code, html, log in my other related question log in with browser and then ruby/mechanize takes it over?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神经暖 2024-08-27 02:25:50

与firefox相比,POST中缺少一个参数导致mechanize无法登录。添加新参数解决了这个问题。所以在我看来,Web 服务器需要 POST 中的 &auth_login=Login 参数。

您可以阅读如何添加新字段以机械化表单< /a> 在另一个问题中。

the absence of one parameter compare to firefox in POST caused mechanize not to log in. Adding new parameter solved this problem. So it seems to me that the web server requires &auth_login=Login parameter to be in POST.

You can read how to add new field to mechanize form in another question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文