2

我正在尝试通过 mechanize 登录到一个站点,我正在填写所有表单,然后提交,但是每次我尝试它时,我最终都会在同一页面上,所以要么我被重定向回来(没有错误消息),或表单未提交。这是为什么?

编码

require 'mechanize'
class Scraper

  def initialize
    @a = Mechanize.new { |agent|
      agent.follow_meta_refresh = true
    }
  end

  def login


    @a.get("https://login.salesforce.com/") do |page|
      red = page.forms.first do |form|
        form.field_with(:type => "email").value = username
        form.field_with(:type => "password").value  =  password
      end.click_button
      pp red
      #puts main.title
    end
  end
end
s = Scraper.new
s.login

初始页面:

   #<Mechanize::Page
   {url #<URI::HTTPS:0x007f8f39d6fb30 URL:https://login.salesforce.com/>}
   {meta_refresh}
   {title "salesforce.com - Customer Secure Login Page"}
   {iframes
    #<Mechanize::Page::Frame
     "marketing"
     "https://www.salesforce.com/login-messages/messages.html?noroundedcorner">}
   {frames}
   {links
    #<Mechanize::Page::Link "Salesforce" "http://www.salesforce.com">
    #<Mechanize::Page::Link
     "Forgot your password?"
     "/secur/forgotpassword.jsp?locale=us">
    #<Mechanize::Page::Link
     "Sign up for free."
     "https://www.salesforce.com/form/trial/freetrial.jsp?d=70130000000Enus">}
   {forms
    #<Mechanize::Form
     {name "login"}
     {method "POST"}
     {action "https://login.salesforce.com/"}
     {fields
      [hidden:0x3fc79cec63ac type: hidden name: un value: ]
      [hidden:0x3fc79cec6244 type: hidden name: width value: ]
      [hidden:0x3fc79cec60c8 type: hidden name: height value: ]
      [hidden:0x3fc79cec5efc type: hidden name: hasRememberUn value: true]
      [hidden:0x3fc79cec5d58 type: hidden name: startURL value: ]
      [hidden:0x3fc79cec5bc8 type: hidden name: loginURL value: ]
      [hidden:0x3fc79cec5a38 type: hidden name: loginType value: ]
      [hidden:0x3fc79cec987c type: hidden name: useSecure value: true]
      [hidden:0x3fc79cec969c type: hidden name: local value: ]
      [hidden:0x3fc79cec9520 type: hidden name: lt value: standard]
      [hidden:0x3fc79cec9340 type: hidden name: qs value: ]
      [hidden:0x3fc79cec9174 type: hidden name: locale value: ]
      [hidden:0x3fc79cec8f80 type: hidden name: oauth_token value: ]
      [hidden:0x3fc79cec8db4 type: hidden name: oauth_callback value: ]
      [hidden:0x3fc79cec8be8 type: hidden name: login value: ]
      [hidden:0x3fc79cec89cc type: hidden name: serverid value: ]
      [hidden:0x3fc79cec8814 type: hidden name: display value: page]
      [field:0x3fc79cec8670 type: email name: username value: ]
      [field:0x3fc79cec84e0 type: password name: pw value: ]}
     {radiobuttons}
     {checkboxes
      [checkbox:0x3fc79cec833c type: checkbox name: rememberUn value: ]}
     {file_uploads}
     {buttons [button:0x3fc79cecac2c type:  name: Login value: ]}>}>

最后一页:

#<Mechanize::Page
{url #<URI::HTTPS:0x007f9d1d250960 URL:https://login.salesforce.com/>}


{meta_refresh}
   {title "salesforce.com - Customer Secure Login Page"}
   {iframes
    #<Mechanize::Page::Frame
     "marketing"
     "https://www.salesforce.com/login-messages/messages.html?    r=https%3A%2F%2Flogin.salesforce.com%2F&noroundedcorner">}
   {frames}
   {links
    #<Mechanize::Page::Link "Salesforce" "http://www.salesforce.com">
    #<Mechanize::Page::Link
     "Forgot your password?"
     "/secur/forgotpassword.jsp?locale=us">
    #<Mechanize::Page::Link
     "Sign up for free."
     "https://www.salesforce.com/form/trial/freetrial.jsp?d=70130000000Enus">}
   {forms
    #<Mechanize::Form
     {name "login"}
     {method "POST"}
     {action "https://login.salesforce.com/"}
     {fields
      [hidden:0x3fce8e93aaac type: hidden name: un value: ]
      [hidden:0x3fce8e93a8a4 type: hidden name: width value: ]
      [hidden:0x3fce8e93a638 type: hidden name: height value: ]
      [hidden:0x3fce8e93a390 type: hidden name: hasRememberUn value: true]
      [hidden:0x3fce8e93a19c type: hidden name: startURL value: null]
      [hidden:0x3fce8e939f58 type: hidden name: loginURL value: null]
      [hidden:0x3fce8e939cc4 type: hidden name: loginType value: ]
      [hidden:0x3fce8e9399a4 type: hidden name: useSecure value: true]
      [hidden:0x3fce8e93979c type: hidden name: local value: ]
      [hidden:0x3fce8e939648 type: hidden name: lt value: standard]
      [hidden:0x3fce8e93d414 type: hidden name: qs value:r=https%3A%2F%2Flogin.salesforce.com%2F]
      [hidden:0x3fce8e93d284 type: hidden name: locale value: ]
      [hidden:0x3fce8e93d0cc type: hidden name: oauth_token value: ]
      [hidden:0x3fce8e93cf50 type: hidden name: oauth_callback value: ]
      [hidden:0x3fce8e93cd98 type: hidden name: login value: ]
      [hidden:0x3fce8e93cc44 type: hidden name: serverid value: ]
      [hidden:0x3fce8e93cab4 type: hidden name: display value: page]
      [field:0x3fce8e93c780 type: email name: username value: ]
      [field:0x3fce8e93c4c4 type: password name: pw value: ]}
     {radiobuttons}
     {checkboxes
      [checkbox:0x3fce8e93c334 type: checkbox name: rememberUn value: ]}
     {file_uploads}
     {buttons [button:0x3fce8e93b9d4 type:  name: Login value: ]}>}>

我的代码有什么问题?

4

2 回答 2

1

该网站使用Javascript处理登录,机械化无法处理。您可以使用Selenium之类的东西来访问该站点。

于 2013-07-24T14:19:30.793 回答
0

在表单上,​​除了包含用户名的字段un之外widthheight还需要包含用户名和一些数字的隐藏字段username

  [hidden:0x3fce8e93aaac type: hidden name: un value: ]
  [hidden:0x3fce8e93a8a4 type: hidden name: width value: ]
  [hidden:0x3fce8e93a638 type: hidden name: height value: ]

您可以在网络选项卡下使用 Chrome 的检查器来监控实际发送到服务器的请求的内容(启用“保留日志”选项),在它被 javascript 修改后。

于 2014-10-14T23:54:23.003 回答